Determining Command-Line vs. HTTP Execution in PHP
In developing PHP scripts, it often becomes necessary to distinguish between execution via the command-line or through HTTP. Output formatting and other aspects of script behavior may differ based on this differentiation.
Canonical Method: php_sapi_name()
The recommended approach to determine the execution mode is to utilize the php_sapi_name() function. It returns the type of interface between the web server and PHP.
if (php_sapi_name() == "cli") { // In cli-mode } else { // Not in cli-mode }
Additional Notes:
The above is the detailed content of How Do I Determine If a PHP Script Is Running From the Command Line or Through HTTP?. For more information, please follow other related articles on the PHP Chinese website!