{
    "slug": "php_offset_not_exists",
    "term": "Undefined Array Key / Offset Errors",
    "category": "php",
    "difficulty": "beginner",
    "short": "Accessing a non-existent array key triggers E_WARNING (PHP 7) or is silently null (PHP 8 with nullsafe) — use isset(), array_key_exists(), or ?? to guard access.",
    "long": "PHP 7 emits E_NOTICE for undefined offset. PHP 8 emits E_WARNING. Neither throws — the expression evaluates to null. Undefined index means string key missing, undefined offset means integer key missing. Best practices: use isset($arr['key']) to check existence, $arr['key'] ?? 'default' for null-coalescing default, array_key_exists() when null is a valid value (isset() returns false for null values). In typed contexts (PHP 8), use dedicated data structures or value objects instead of raw arrays to prevent key access errors.",
    "aliases": [],
    "tags": [
        "php",
        "arrays",
        "errors",
        "null"
    ],
    "misconception": "isset() and array_key_exists() are equivalent — isset() returns false for null values while array_key_exists() returns true. Use the right one.",
    "why_it_matters": "Silent null from undefined key propagates through calculations causing type errors or data corruption much later than the actual bug.",
    "common_mistakes": [
        "Using $arr['key'] without isset() check — produces warning and null.",
        "Confusing isset() with array_key_exists() when null is a valid array value.",
        "Not enabling E_WARNING in development — errors become invisible."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "null_coalescing",
        "php_type_error",
        "php_error_levels",
        "nullsafe_operator"
    ],
    "prerequisites": [
        "null_coalescing"
    ],
    "refs": [
        "https://www.php.net/manual/en/function.array-key-exists.php"
    ],
    "bad_code": "$config = ['debug' => true];\necho $config['database']; // Warning: Undefined array key 'database'",
    "good_code": "// Null coalescing:\n$db = $config['database'] ?? 'mysql://localhost/app';\n\n// When null is a valid value:\nif (array_key_exists('database', $config)) {\n    $db = $config['database']; // could be null\n}\n\n// PHP 8.1+ — first-class enums/value objects are better than raw arrays",
    "quick_fix": "Use $arr['key'] ?? $default for simple defaults. Use array_key_exists() when null is valid. Enable E_ALL in dev to surface all undefined key warnings.",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_offset_not_exists",
        "html_url": "https://codeclaritylab.com/glossary/php_offset_not_exists",
        "json_url": "https://codeclaritylab.com/glossary/php_offset_not_exists.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 Array Key / Offset Errors](https://codeclaritylab.com/glossary/php_offset_not_exists) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_offset_not_exists"
            }
        }
    }
}