PHP Data Types


There are different data types for different purposes. PHP does not have explicit type definitions, but the type of a variable is determined by the type of the value that is assigned, or by the type that it is casted to. This is a brief overview about the types, for a detailed documentation and examples, see the PHP types topic.

There are following data types in PHP: null, boolean, integer, float, string, object, resource and array.


Null can be assigned to any variable. It represents a variable with no value.

$foo = null;

This invalidates the variable and it's value would be undefined or void if called. The variable is cleared from memory and deleted by the garbage collector.


This is the simplest type with only two possible values.

$foo = true;
$bar = false;

Booleans can be used to control the flow of code.

$foo = true;

if ($foo) {
    echo "true";
} else {
    echo "false";


An integer is a whole number positive or negative. It can be in used with any number base. The size of an integer is platform-dependent. PHP does not support unsigned integers.

$foo = -3;  // negative
$foo = 0;   // zero (can also be null or false (as boolean)
$foo = 123; // positive decimal
$bar = 0123; // octal = 83 decimal
$bar = 0xAB; // hexadecimal = 171 decimal
$bar = 0b1010; // binary = 10 decimal
var_dump(0123, 0xAB, 0b1010); // output: int(83) int(171) int(10)


Floating point numbers, "doubles" or simply called "floats" are decimal numbers.

$foo = 1.23;
$foo = 10.0;
$bar = -INF;
$bar = NAN;


An array is like a list of values. The simplest form of an array is indexed by integer, and ordered by the index, with the first element lying at index 0.

$foo = array(1, 2, 3); // An array of integers
$bar = ["A", true, 123 => 5]; // Short array syntax, PHP 5.4+

echo $bar[0];    // Returns "A"
echo $bar[1];    // Returns true
echo $bar[123];  // Returns 5
echo $bar[1234]; // Returns null

Arrays can also associate a key other than an integer index to a value. In PHP, all arrays are associative arrays behind the scenes, but when we refer to an 'associative array' distinctly, we usually mean one that contains one or more keys that aren't integers.

$array = array();
$array["foo"] = "bar";
$array["baz"] = "quux";
$array[42] = "hello";
echo $array["foo"]; // Outputs "bar"
echo $array["bar"]; // Outputs "quux"
echo $array[42]; // Outputs "hello"


A string is like an array of characters.

$foo = "bar";

Like an array, a string can be indexed to return its individual characters:

$foo = "bar";
echo $foo[0]; // Prints 'b', the first character of the string in $foo.


An object is an instance of a class. Its variables and methods can be accessed with the -> operator.

$foo = new stdClass(); // create new object of class stdClass, which a predefined, empty class
$foo->bar = "baz";
echo $foo->bar; // Outputs "baz"
// Or we can cast an array to an object:
$quux = (object) ["foo" => "bar"];
echo $quux->foo; // This outputs "bar".


Resource variables hold special handles to opened files, database connections, streams, image canvas areas and the like (as it is stated in the manual).

$fp = fopen('file.ext', 'r'); // fopen() is the function to open a file on disk as a resource.
var_dump($fp); // output: resource(2) of type (stream)

To get the type of a variable as a string, use the gettype() function:

echo gettype(1); // outputs "integer"
echo gettype(true); // "boolean"