devnotes.

PHP Data Types Overview

PHP has eight primitive types, grouped into four categories:

Scalar types:

  • int — whole numbers: $count = 42;
  • float (or double) — decimal numbers: $price = 9.99;
  • string — text: $name = 'Bob';
  • bool — true or false

Compound types:

  • array — ordered maps: $fruits = ['apple', 'banana'];
  • object — instances of classes

Special types:

  • null — represents 'no value'
  • resource — references to external resources (file handles, database connections)

Check a variable's type with gettype($var) or var_dump($var) (the latter also shows the value, and is invaluable for debugging). Test for a specific type with is_int(), is_string(), is_array(), is_null(), etc.

$x = 10;
var_dump($x);        // int(10)
var_dump((string) $x); // string(2) "10"
var_dump(is_int($x)); // bool(true)

Since PHP 7.0, you can add optional type declarations to function parameters and return values (e.g. function add(int $a, int $b): int), and since PHP 8.0+ union types like int|string are supported, giving PHP an increasingly strong, optional type system on top of its dynamic core.

PHP Data Types: Complete Overview | DevNotes