PHPData Types Overview

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
<?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 as PHP_INT_MAX.

  • float — a number with a decimal point or in scientific notation; PHP does not distinguish float from double internally.

  • string — a sequence of characters, written with single quotes, double quotes, or heredoc/nowdoc syntax.

  • bool — exactly two possible values, true or false.

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
<?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 keyword null, 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
<?php
$middleName = null;
var_dump($middleName);

$handle = fopen('data.txt', 'r');
var_dump($handle);
fclose($handle);
NULL
resource(3) of type (stream)
resource is fading out
As of PHP 8.0+, several extensions (such as cURL and, in PHP 8.1, several others) have migrated from `resource` handles to proper `object` instances internally. The `resource` type still exists and you will encounter it, but it is gradually being phased out across the standard library in favor of typed objects.
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
<?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);   // NULL
int(0)
float(0)
string(1) "0"
bool(false)
NULL
These five values look interchangeable but are not
`0`, `0.0`, `"0"`, `false`, and `null` are all "falsy" in a boolean context, so a careless `if ($value)` check treats them identically. But they are five distinct types, and a loose comparison like `0 == "abc"` or `null == false` can produce surprising results. When the exact type matters, compare with `===` instead of `==`, and use `var_dump()` rather than `echo` when you are unsure what you are actually holding.
Summary table

Category

Types

Example

Scalar

int, float, string, bool

42, 3.14, "hi", true

Compound

array, object

[1, 2, 3], new Point(0, 0)

Special

null, resource

null, an open file handle

Tip
When you are not sure what type a variable holds, reach for `var_dump($value)` first — it tells you the type, the length (for strings), and the full nested structure (for arrays and objects) in one call, which is far more informative than printing the value with `echo` alone.