{
    "slug": "php_data_types",
    "term": "PHP Data Types",
    "category": "php",
    "difficulty": "beginner",
    "short": "PHP's eight primitive types — bool, int, float, string, array, object, callable, null — and how the type system coerces between them.",
    "long": "PHP is dynamically typed with eight primitive types: bool, int, float (double), string, array, object, callable, and null. PHP 8.0 formalised mixed (any type) and never (no return). Type coercion rules are nuanced: '1' == 1 is true, '0' == false is true, but null == false and null == 0 are also true. PHP 8.0 changed 0 == 'foo' to false (was true in PHP 7). Typed properties (PHP 7.4+), union types (8.0), and strict_types enforce types at runtime. Understanding coercion rules is critical for security (authentication bypass via type juggling) and correctness.",
    "aliases": [
        "PHP types",
        "PHP type system",
        "PHP scalar types"
    ],
    "tags": [
        "php",
        "type-system",
        "basics"
    ],
    "misconception": "PHP is a loosely typed language so type declarations are optional extras. Since PHP 7, scalar type declarations and return types are enforced — and with declare(strict_types=1) PHP rejects all implicit coercions, making it behave like a strictly typed language at call boundaries.",
    "why_it_matters": "PHP has eight primitive types and loose type juggling between them — understanding how types coerce ensures predictable behaviour and prevents the class of bugs that strict_types=1 is designed to catch.",
    "common_mistakes": [
        "Not enabling strict_types=1 — PHP silently coerces '42' to 42 for int parameters without it.",
        "Using == to compare values of different types — '0' == false == null == 0 in loose comparison.",
        "Forgetting that array is a first-class type in PHP, not an object — json_encode behaviour differs.",
        "Using float for currency — floating-point precision errors accumulate; use integer cents or bcmath."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "type_juggling",
        "type_coercion",
        "strict_types",
        "union_types"
    ],
    "prerequisites": [
        "type_declarations",
        "php_casting",
        "strict_types"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.types.php"
    ],
    "bad_code": "// Loose comparison type confusion:\nvar_dump(0 == 'a');     // true in PHP 7, false in PHP 8 — changed!\nvar_dump('' == false);  // true\nvar_dump('0' == false); // true\nvar_dump(0 == null);    // true\n// Use === always for predictable comparisons",
    "good_code": "// PHP 8 scalar types:\n\\$int   = 42;          // PHP_INT_MAX = 9223372036854775807 on 64-bit\n\\$float = 3.14;        // IEEE 754 double — avoid for money (use int cents)\n\\$str   = 'hello';     // byte string — mb_* functions for Unicode\n\\$bool  = true;\n\n// Compound:\n\\$arr   = [1, 'two', 3.0];              // ordered map\n\\$assoc = ['key' => 'value'];\n\\$obj   = new stdClass();\n\\$null  = null;\n\n// PHP 8 type system additions:\n// union: int|string\n// intersection: Iterator&Countable (8.1)\n// never, void, mixed\n// Enums with backed types (8.1)\n\n// Type juggling changes in PHP 8:\nvar_dump(0 == 'foo'); // false in PHP 8 (was true in PHP 7)\nvar_dump(0 == '');    // false in PHP 8 (was true in PHP 7)",
    "quick_fix": "Use PHP's type system fully: declare return types, use int over float for counts, use bool not int for flags, and use null explicitly with nullable types",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_data_types",
        "html_url": "https://codeclaritylab.com/glossary/php_data_types",
        "json_url": "https://codeclaritylab.com/glossary/php_data_types.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[PHP Data Types](https://codeclaritylab.com/glossary/php_data_types) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_data_types"
            }
        }
    }
}