Data Types in PHP

From the PHP cheat sheet · Setup & Basics · verified Jul 2026

Data Types

PHP data types and type declarations

php
// Scalar types
$string = "text";
$int = 42;
$float = 3.14;
$bool = true;

// Compound types
$array = [1, 2, 3];
$object = new stdClass();

// Type declarations (PHP 7+)
function add(int $a, int $b): int {
    return $a + $b;
}

// Nullable types (PHP 7.1+)
function find(?int $id): ?User {
    return $id ? User::find($id) : null;
}

// Union types (PHP 8.0+)
function process(int|float $number): string|false {
    return $number > 0 ? "positive" : false;
}
💡 PHP is dynamically typed but supports type hints
⚡ Use strict_types=1 for type safety
📌 Union types allow multiple possible types
🔥 Never type indicates function never returns

More PHP tasks

Back to the full PHP cheat sheet