{
    "slug": "php_undefined_variable",
    "term": "Undefined Variable and Null Coalescing Fixes",
    "category": "php",
    "difficulty": "beginner",
    "short": "Accessing an undefined variable in PHP returns null and triggers an E_WARNING — use isset(), empty(), or ?? to check safely before use.",
    "long": "PHP does not require variable declaration before use. Accessing a variable that has never been assigned returns null and triggers an E_NOTICE (PHP 5) or E_WARNING (PHP 7+). PHP 8 promoted this to a warning, making it more visible. The fix depends on context: use isset($x) before conditional use, $x ?? 'default' for a fallback value, or null coalescing assignment $x ??= 'default' to initialise only if not set. With PHPStan at level 6+, many undefined variable accesses are caught statically.",
    "aliases": [
        "E_NOTICE undefined variable",
        "undefined index",
        "undefined offset"
    ],
    "tags": [
        "php",
        "debugging",
        "null",
        "variables",
        "errors"
    ],
    "misconception": "Undefined variable errors are harmless notices — in PHP 8 they are warnings, and they often signal logic bugs where a variable was expected to be set but wasn't.",
    "why_it_matters": "Silencing undefined variable notices hides real bugs — using ?? and isset() everywhere makes intent explicit and eliminates an entire class of runtime warnings.",
    "common_mistakes": [
        "Accessing $_GET['key'] without isset() or ??",
        "Using a loop variable after the loop where it may not be set",
        "Not initialising accumulator variables before loops",
        "Relying on PHP returning null silently rather than making intent explicit"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "null_coalescing",
        "nullsafe_operator",
        "input_validation",
        "php_error_levels"
    ],
    "prerequisites": [
        "null_coalescing",
        "input_validation"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.variables.php"
    ],
    "bad_code": "<?php\necho $username; // Notice: Undefined variable",
    "good_code": "<?php\n$username = $_SESSION['username'] ?? 'Guest';\necho $username; // Always defined\n\n// Or check first:\nif (isset($username)) { echo $username; }",
    "quick_fix": "Enable PHPStan level 4+ which detects potentially undefined variables, and use $x ?? 'default' everywhere an array key or variable might not exist",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_undefined_variable",
        "html_url": "https://codeclaritylab.com/glossary/php_undefined_variable",
        "json_url": "https://codeclaritylab.com/glossary/php_undefined_variable.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": "[Undefined Variable and Null Coalescing Fixes](https://codeclaritylab.com/glossary/php_undefined_variable) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_undefined_variable"
            }
        }
    }
}