Data Types Overview
Every value in PHP has a type, even though you never write that type down when declaring a variable. PHP defines eight built-in types in total, and understanding how they group together — scalar values, compound structures, and a couple of special cases — makes it much easier to predict how a piece of data will behave in a comparison, a loop, or a function call. This page is a map of that whole type system before we go deeper into each one individually.
Scalar types
A scalar type holds a single, indivisible value. PHP has four of them: int, float, string, and bool.
The four scalar types
<?php $age = 34; // int $price = 19.99; // float (also called "double") $name = "Priya"; // string $isMember = true; // bool var_dump($age, $price, $name, $isMember);
int(34) float(19.99) string(5) "Priya" bool(true)
int— a whole number, positive or negative, with a platform-dependent maximum size available asPHP_INT_MAX.float— a number with a decimal point or in scientific notation; PHP does not distinguishfloatfromdoubleinternally.string— a sequence of characters, written with single quotes, double quotes, or heredoc/nowdoc syntax.bool— exactly two possible values,trueorfalse.
Compound types
A compound type can hold more than one value, or a value together with behavior. PHP has two: array and object.
array and object
<?php
$colors = ["red", "green", "blue"]; // array
class Point
{
public function __construct(
public float $x,
public float $y
) {}
}
$origin = new Point(0.0, 0.0); // object
var_dump($colors);
var_dump($origin);array(3) {
[0]=>
string(3) "red"
[1]=>
string(5) "green"
[2]=>
string(4) "blue"
}
object(Point)#1 (2) {
["x"]=>
float(0)
["y"]=>
float(0)
}PHP arrays are actually ordered maps under the hood — they work as numerically indexed lists, associative dictionaries, or both at once, which is why they turn up everywhere in PHP code. Objects are instances of classes and carry both data (properties) and behavior (methods); they are covered in full on the object-oriented programming pages.
Special types
Two more types round out the list, and they behave differently enough from everything else that PHP groups them as special types: null and resource.
null— a type with exactly one possible value, written as the keywordnull, representing "no value at all."resource— an opaque handle to an external resource such as an open file, a database connection, or an image handle, managed by an extension rather than by ordinary PHP code.
null and resource
<?php
$middleName = null;
var_dump($middleName);
$handle = fopen('data.txt', 'r');
var_dump($handle);
fclose($handle);NULL resource(3) of type (stream)
Inspecting a value with var_dump()
var_dump() is the single most useful debugging function for understanding types, because unlike echo or print_r(), it prints both the type name and the value — including the exact string length and, for arrays and objects, every nested element.
Comparing similar-looking values
<?php
var_dump(0); // int(0)
var_dump(0.0); // float(0)
var_dump("0"); // string(1) "0"
var_dump(false); // bool(false)
var_dump(null); // NULLint(0) float(0) string(1) "0" bool(false) NULL
Summary table
Category | Types | Example |
|---|---|---|
Scalar |
|
|
Compound |
|
|
Special |
|
|