{
    "slug": "php_sapi_types",
    "term": "PHP SAPI Types (FPM, CLI, Embed)",
    "category": "php",
    "difficulty": "intermediate",
    "short": "SAPI (Server API) defines how PHP is invoked — php-fpm for web requests, php-cli for commands, embed for C extensions — each has different lifecycle and configuration.",
    "long": "PHP SAPIs: php-fpm (FastCGI Process Manager) — long-running pool of workers handling web requests, separate PHP-FPM pool config, per-request memory reset. php-cli — single execution, unlimited max_execution_time by default, used for scripts, cron, queues. php-cgi — legacy CGI, spawned per request. apache2handler — mod_php embedded in Apache. embed — PHP embedded in C apps. php_sapi_name() returns the SAPI name. Key differences: superglobals ($_SERVER, $_REQUEST) only populated in web SAPIs. STDIN/STDOUT available in CLI. php-fpm pools can have per-pool php.ini overrides. Workers share nothing between requests — no state persistence without external storage (Redis, DB).",
    "aliases": [],
    "tags": [
        "php",
        "sapi",
        "fpm",
        "cli",
        "server"
    ],
    "misconception": "PHP-FPM workers persist state between requests — each request gets a fresh PHP environment. State must be stored externally.",
    "why_it_matters": "Understanding SAPIs prevents SAPI-specific code from being used in the wrong context and helps configure each correctly for performance and security.",
    "common_mistakes": [
        "Using CLI-only functions (readline, pcntl) in web context.",
        "Not setting different memory_limit/max_execution_time per SAPI.",
        "Assuming $_SERVER is populated in CLI scripts."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_fpm",
        "php_cli",
        "php_ini",
        "php_error_levels"
    ],
    "prerequisites": [
        "php_fpm",
        "php_cli"
    ],
    "refs": [
        "https://www.php.net/manual/en/function.php-sapi-name.php"
    ],
    "bad_code": "// In web request — reads from stdin, only works in CLI:\n$input = fgets(STDIN); // Returns false in FPM",
    "good_code": "// SAPI detection:\nif (PHP_SAPI === 'cli') {\n    $input = fgets(STDIN);\n} else {\n    $input = $_POST['data'] ?? '';\n}\n\n// Check in code:\necho PHP_SAPI; // 'fpm-fcgi', 'cli', 'apache2handler' etc",
    "quick_fix": "Use PHP_SAPI constant to conditionally run SAPI-specific code. Configure separate php.ini pools for FPM vs CLI with appropriate limits.",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_sapi_types",
        "html_url": "https://codeclaritylab.com/glossary/php_sapi_types",
        "json_url": "https://codeclaritylab.com/glossary/php_sapi_types.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 SAPI Types (FPM, CLI, Embed)](https://codeclaritylab.com/glossary/php_sapi_types) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_sapi_types"
            }
        }
    }
}