{
    "slug": "php_stack_overflow",
    "term": "Stack Overflow from Deep Recursion",
    "category": "php",
    "difficulty": "intermediate",
    "short": "PHP has no configurable stack size limit — deep recursion causes a fatal segfault or memory exhaustion, not a catchable exception.",
    "long": "PHP's call stack is limited by the OS stack size (typically 8MB on Linux). Deep recursion exhausts it and causes a segfault or memory_limit error — neither is catchable with try/catch. Practical limit is roughly 500–5000 nested calls depending on frame size. Solutions: convert tail-recursive functions to iterative loops, use a trampoline pattern, or process trees iteratively with an explicit stack (SplStack). xdebug.max_nesting_level (default 256) provides an earlier, catchable limit via E_ERROR.",
    "aliases": [],
    "tags": [
        "php",
        "recursion",
        "performance",
        "errors"
    ],
    "misconception": "PHP will throw a catchable exception on stack overflow — it segfaults or hits memory_limit, both of which are uncatchable fatal errors.",
    "why_it_matters": "Recursive algorithms on user-supplied data (like parsing nested JSON or tree structures) can be exploited to crash PHP via stack exhaustion.",
    "common_mistakes": [
        "Recursive tree traversal without depth limit.",
        "Not converting obvious tail recursion to iteration.",
        "Setting xdebug.max_nesting_level too high — delays the crash."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_oom_error",
        "recursion_patterns",
        "generators"
    ],
    "prerequisites": [
        "recursion_patterns"
    ],
    "refs": [
        "https://www.php.net/manual/en/function.xdebug-get-stack-depth.php"
    ],
    "bad_code": "function factorial(int $n): int {\n    return $n <= 1 ? 1 : $n * factorial($n - 1);\n}\nfactorial(100000); // Segfault",
    "good_code": "function factorial(int $n): int {\n    $result = 1;\n    for ($i = 2; $i <= $n; $i++) $result *= $i;\n    return $result;\n}\n\n// Iterative tree traversal using explicit stack\nfunction traverse(Node $root): void {\n    $stack = new \\SplStack();\n    $stack->push($root);\n    while (!$stack->isEmpty()) {\n        $node = $stack->pop();\n        process($node);\n        foreach ($node->children as $child) $stack->push($child);\n    }\n}",
    "quick_fix": "Convert recursive functions to iterative using SplStack or a while loop. Set xdebug.max_nesting_level=500 to get an early catchable error in dev.",
    "severity": "high",
    "effort": "medium",
    "created": "2026-03-22",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_stack_overflow",
        "html_url": "https://codeclaritylab.com/glossary/php_stack_overflow",
        "json_url": "https://codeclaritylab.com/glossary/php_stack_overflow.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": "[Stack Overflow from Deep Recursion](https://codeclaritylab.com/glossary/php_stack_overflow) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_stack_overflow"
            }
        }
    }
}