{
    "slug": "php_max_execution",
    "term": "max_execution_time & set_time_limit()",
    "category": "php",
    "difficulty": "beginner",
    "short": "max_execution_time limits script CPU time (not wall-clock time) — use set_time_limit(0) sparingly for long-running CLI tasks, never for web requests.",
    "long": "max_execution_time (default 30s) measures CPU time consumed by the PHP process, not wall-clock time. Blocking I/O (sleep, DB queries, HTTP requests) does NOT count. This means a script making many slow DB calls can run for minutes without hitting the limit. set_time_limit(N) resets the timer from the current point. CLI scripts have max_execution_time=0 (unlimited) by default. The fatal error it produces is uncatchable. For long operations: use background jobs/queues, call set_time_limit() in loops, or use CLI with time limits managed by the OS.",
    "aliases": [],
    "tags": [
        "php",
        "performance",
        "configuration"
    ],
    "misconception": "max_execution_time is wall-clock time — it only counts CPU time. A script sleeping or waiting for I/O can run indefinitely regardless of the setting.",
    "why_it_matters": "Runaway scripts consuming CPU without hitting time limits can starve servers. Conversely, legitimate long-running imports may need explicit set_time_limit() calls.",
    "common_mistakes": [
        "Using set_time_limit(0) in web requests — allows infinite execution.",
        "Assuming max_execution_time protects against slow DB queries.",
        "Not using background jobs for operations known to exceed 30s."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_oom_error",
        "async_processing",
        "php_queue_workers",
        "php_cli"
    ],
    "prerequisites": [
        "php_cli"
    ],
    "refs": [
        "https://www.php.net/manual/en/function.set-time-limit.php"
    ],
    "bad_code": "// In a web controller:\nset_time_limit(0); // Disable limit for 'safety'\nforeach ($millionRows as $row) { processRow($row); }",
    "good_code": "// In a CLI command or queue worker:\nset_time_limit(3600); // 1 hour for this operation\n\n// Reset per chunk to avoid single-chunk timeout:\nforeach (array_chunk($rows, 500) as $chunk) {\n    set_time_limit(60); // 60s per chunk\n    processChunk($chunk);\n}",
    "quick_fix": "Move long operations to CLI/queue workers. In loops, call set_time_limit(60) per iteration. Never use set_time_limit(0) in web request handlers.",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-22",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_max_execution",
        "html_url": "https://codeclaritylab.com/glossary/php_max_execution",
        "json_url": "https://codeclaritylab.com/glossary/php_max_execution.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": "[max_execution_time & set_time_limit()](https://codeclaritylab.com/glossary/php_max_execution) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_max_execution"
            }
        }
    }
}