{
    "slug": "php_compilation_pipeline",
    "term": "PHP Compilation Pipeline",
    "category": "compiler",
    "difficulty": "advanced",
    "short": "Source code → lexer → tokens → parser → AST → compiler → opcodes → Zend VM — OPcache intercepts after compilation to cache and reuse opcodes across requests.",
    "long": "PHP execution pipeline: (1) Lexer: source text → token stream (T_FUNCTION, T_STRING, T_WHITESPACE). (2) Parser: tokens → Abstract Syntax Tree using LALR(1) grammar. (3) Compiler: AST → opcode array (zend_op structs — ZEND_ADD, ZEND_CALL, ZEND_RETURN). (4) Zend VM: executes the opcode array. OPcache intercepts after step 3: serialises compiled opcodes to shared memory — subsequent requests fetch opcodes directly, skipping steps 1-3. JIT (PHP 8.0+): OPcache's JIT further compiles hot opcodes to native x86/ARM machine code.",
    "aliases": [
        "PHP pipeline",
        "Zend VM",
        "opcode compilation",
        "PHP compiler internals"
    ],
    "tags": [
        "compiler",
        "php",
        "performance"
    ],
    "misconception": "PHP is interpreted line-by-line — PHP compiles the entire file to opcodes before executing any of it; the 'interpreted' label refers to the absence of AOT native compilation, not line-by-line execution.",
    "why_it_matters": "OPcache provides 2-5x speedup by skipping lexing, parsing, and compilation on every request; syntax errors appear before any code runs (they are compilation errors); JIT helps CPU-bound code but not I/O-bound web requests.",
    "common_mistakes": [
        "Assuming PHP re-lexes and re-parses files on every request without OPcache",
        "Expecting syntax errors to appear lazily at the line they are on — errors appear at compile time",
        "Disabling OPcache in production — the single biggest PHP performance regression possible",
        "Thinking OPcache opcodes are the same as JIT-compiled native code — they are different stages"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "jit_compilation_deep",
        "php_opcache_tuning",
        "opcode_optimisation",
        "aot_vs_jit"
    ],
    "prerequisites": [
        "bytecode_vm",
        "opcache",
        "php_jit"
    ],
    "refs": [
        "https://www.php.net/manual/en/internals2.opcodes.php"
    ],
    "bad_code": "// Expecting lazy evaluation:\nif (false) {\n    $x = 1 +; // Syntax error on this line\n}\n// Assumption: no error — dead branch\n// Reality: PHP compiles the entire file first\n// Fatal error: parse error, unexpected ';' — before any code executes",
    "good_code": "// OPcache eliminates recompilation:\n// Request 1 (cold): lex → parse → compile → store in shared memory\n// Request 2 (warm): fetch opcodes from shared memory → execute directly\n// Speedup: skip ~90% of compilation work per request\n\n// Verify OPcache is working:\n$status = opcache_get_status();\n// Healthy production: hit rate > 99%\n// opcache_hit_rate = hits / (hits + misses)",
    "quick_fix": "Understanding the pipeline explains OPcache's value: tokenise → parse → compile opcodes happens once; opcache stores the result so subsequent requests execute immediately",
    "severity": "info",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_compilation_pipeline",
        "html_url": "https://codeclaritylab.com/glossary/php_compilation_pipeline",
        "json_url": "https://codeclaritylab.com/glossary/php_compilation_pipeline.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 Compilation Pipeline](https://codeclaritylab.com/glossary/php_compilation_pipeline) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_compilation_pipeline"
            }
        }
    }
}