Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

Variable Types

PHP does not require a command to declare a variable, and the data type is determined by the value assigned to the variable.

Example

$x = 5;      // $x is an integer
$y = "John"; // $y is a string
echo $x;
echo $y;

PHP supports the following data types:

  • String
  • Integer
  • Float (floating-point numbers, also known as double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

Get the Type

To determine the data type of a variable, use the var_dump() function.

Example

The var_dump() function provides both the data type and the value.

$x = 5;
var_dump($x);

Example

Check what var_dump() returns for different data types:

var_dump(5);
var_dump("John");
var_dump(3.14);
var_dump(true);
var_dump([2, 3, 56]);
var_dump(NULL);

Assign String to a Variable

To assign a string to a variable, use the variable name followed by an equal sign and the string.

Example

$x = "John";
echo $x;

Assign Multiple Values

You can assign the same value to multiple variables in a single line.

Example

All three variables receive the value “Fruit”:

$x = $y = $z = "Fruit";