PHP SAPI Types (FPM, CLI, Embed)
debt(d7/e3/b3/t7)
Closest to 'only careful code review or runtime testing' (d7), since PHPStan listed in detection_hints won't reliably catch CLI-only functions used in web context or assumptions about $_SERVER population; issues typically surface at runtime in the wrong SAPI.
Closest to 'simple parameterised fix' (e3), since quick_fix is a PHP_SAPI conditional guard plus separate php.ini pool configuration — small pattern replacement but touches both code and config.
Closest to 'localised tax' (b3), since SAPI-awareness affects bootstrap/entrypoint code and config but doesn't shape the whole codebase; most business logic is SAPI-agnostic.
Closest to 'serious trap' (t7), since the misconception (FPM workers persist state between requests) contradicts how long-running app servers in other ecosystems work (Node, Python ASGI, Java), leading developers to wrongly cache state in globals.
TL;DR
Explanation
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).
Common Misconception
Why It Matters
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.
Code Examples
// In web request — reads from stdin, only works in CLI:
$input = fgets(STDIN); // Returns false in FPM
// SAPI detection:
if (PHP_SAPI === 'cli') {
$input = fgets(STDIN);
} else {
$input = $_POST['data'] ?? '';
}
// Check in code:
echo PHP_SAPI; // 'fpm-fcgi', 'cli', 'apache2handler' etc