Every single expression in PHP has one of the following built-in types depending on its value:
PHP is a dynamically typed language, which means that by default there is no need to specify the type of a variable, as this will be determined at runtime.
However, it is possible to statically type some aspect of the language via the use of type declarations. Type declarations can be added to function arguments, return values, class properties, class constants. They ensure that the value is of the specified type at call time, otherwise a TypeError is thrown.
Types restrict the kind of operations that can be performed on them. However, if an expression/variable is used in an operation which its type does not support, PHP will attempt to type juggle the value into a type that supports the operation. This process depends on the context in which the value is used.
Null
null represents a variable with no value. It is often used to indicate the absence of data or an uninitialized state.
$value = null; if ($value === null) { echo "Value is null"; }
Booleans
A boolean can be either true or false. It is mainly used in conditional statements and logical operations.
$isActive = true; if ($isActive) { echo "The flag is true"; }
Integers
Integers are whole numbers, positive or negative. Underscores can be used for readability.
$age = 30; $bigNumber = 1_000_000; echo $age + $bigNumber; // 1000030
Floats
Floats represent numbers with decimal points. Underscores can also improve readability in float literals.
$price = 19.99; $pi = 3.141_592; echo $price * $pi; // 62.80042408
Strings
Strings represent sequences of characters. They can be written with single quotes, double quotes, heredoc, or nowdoc syntax.
$single = 'Hello'; $double = "${single} World"; $heredoc = <<<TEXT This is a heredoc string. TEXT; $nowdoc = <<<'TEXT' This is a nowdoc string. TEXT;
Numeric Strings
Numeric strings are strings that contain valid numbers. PHP can automatically convert them into numbers when needed.
$numString = "42"; $sum = $numString + 8; // automatically converted to int echo $sum; // 50
Arrays
Arrays are collections of values that can be indexed by numbers or strings. They are used to group related data.
$colors = ["red", "green", "blue"]; echo $colors[1]; // green
Objects
Objects are instances of classes that bundle data (properties) and behavior (methods).
class Car { public $brand; public function __construct($brand) { $this->brand = $brand; } } $car = new Car("Toyota"); echo $car->brand; // Toyota
Enumerations
Enums define a set of possible values for a variable. They make code safer and more readable.
enum Status { case Active; case Inactive; } $status = Status::Active; echo $status->name; // Active
Resources
Resources are special variables holding references to external resources (like files, streams, database connections).
$file = fopen("example.txt", "w"); fwrite($file, "Hello World"); fclose($file);
Callbacks / Callables
Callables are variables that can be used as functions (e.g., anonymous functions or function names).
function greet($name) { return "Hello, $name!"; } $callback = "greet"; echo $callback("Alice"); // Hello, Alice!
Mixed
mixed is a type that accepts any value. It is useful when a function can work with multiple different types. Mixed is, in type theory parlance, the top type. Meaning every other type is a subtype of it.
function process(mixed $input): void { var_dump($input); } process("text"); process(123); process([1,2,3]);
Void
void indicates that a function does not return any value.
function logMessage(string $msg): void { echo $msg; } logMessage("Logging...");
Never
never indicates that a function never returns (for example, it throws an exception or exits). Never is, in type theory parlance, the bottom type. Meaning it is the subtype of every other type and can replace any other return type during inheritance.
function fail(string $msg): never { throw new Exception($msg); } fail("Something went wrong");
Relative Class Types
These keywords refer to class-related types: self (the current class), parent (the parent class), static (the called class).
class Base { public function create(): self { return new self(); } } class Child extends Base { public function create(): static { return new static(); } }
Singleton Types
You can declare return types as true or false specifically, not just bool.
function alwaysTrue(): true { return true; } echo alwaysTrue();
Iterables
iterable is a type that accepts arrays or any object implementing Traversable. It’s useful for functions that loop over items.
function printItems(iterable $items): void { foreach ($items as $item) { echo $item; } } printItems([1, 2, 3]);
Source: Orkhan Alishov's notes