php_sapi_name
php_sapi_name(): string|false
Returns the lowercase name of the hosting interface (SAPI) - "cli", "fpm-fcgi", "apache2handler", etc.; the PHP_SAPI constant holds the same value, letting one script tell its CLI and web forms apart.
C C23
#include <stdio.h>
int main(void) {
/* C is a single-form language: the program IS the process */
printf("single-form binary: no SAPI concept, main() is the entry\n");
return 0;
}
single-form binary: no SAPI concept, main() is the entry
C++ C++26
#include <iostream>
int main() {
// C++ is a single-form language: the program IS the process
std::cout << "single-form binary: no SAPI concept, main() is the entry\n";
return 0;
}
single-form binary: no SAPI concept, main() is the entry
C# v11.0.100
// C# always runs inside the CLR: the runtime is the host, top-level statements are the entry
Console.WriteLine("host: clr - top-level statements are the entry");
host: clr - top-level statements are the entry
Go v1.27.4
package main
import "fmt"
func main() {
// Go compiles to a single-form binary: the process itself is the only "host"
fmt.Println("single-form binary: no SAPI duality, main() is the entry")
}
single-form binary: no SAPI duality, main() is the entry
Java v27.0.1
public class Main {
public static void main(String[] args) {
// Java always runs inside the JVM: the VM is the host, main() is the entry
System.out.println("host: jvm - main() is the entry");
}
}
host: jvm - main() is the entry
JavaScript v26.13.1
// JS: no first-class host API - probe the globals to distinguish Node from browser
const host = typeof process !== "undefined" && process.versions?.node ? "node" : "browser";
console.log(`host: ${host}`);
host: node
PHP v8.6.0
// php_sapi_name tells which SAPI hosts this script; PHP_SAPI is the constant twin
$sapi = php_sapi_name();
echo "SAPI: ", $sapi, "\n"; // "cli" in this terminal run
var_dump($sapi === PHP_SAPI);
SAPI: cli bool(true)
Python v3.15.2
# Python: no host duality - the interpreter is always the host; __main__ marks the entry
print(f"entry module: {__name__}")
entry module: __main__
Rust v1.99.1
fn main() {
// Rust compiles to a single-form binary: no host abstraction exists at all
println!("single-form binary: no SAPI concept, main() is the entry");
}
single-form binary: no SAPI concept, main() is the entry
TypeScript v26.13.1
// TS: same host probing as JS (Node strips the type annotations at runtime)
const isNode: boolean = typeof process !== "undefined" && process.versions?.node !== undefined;
console.log(`host: ${isNode ? "node" : "browser"}`);
host: node
Language differences
C: a single-form language - the program IS the process and main() is the entry; no host duality.
C++: same as C - the compiled artifact is the process itself.
C#: runs inside the CLR; the runtime is the "host" and there is no second form.
Go: compiles to a single-form static binary; the process itself is the only "host".
Java: runs inside the JVM; System.getProperty("java.vm.name") can name the VM implementation, but the host form never changes.
JavaScript: no official API - probe the globals to distinguish Node/browser/Deno etc.
PHP: common values are cli, cli-server, fpm-fcgi, apache2handler, cgi-fcgi; detect the CLI with PHP_SAPI === 'cli'; the same file sees wildly different $_SERVER contents under CLI vs web - the SAPI check is the first step of any branching behavior (see the phpinfo entry).
Python: the interpreter is always the host; __name__ == "__main__" detects the entry module, not the host form.
Rust: same as C - no host abstraction.
TypeScript: same as JavaScript.