PHP - how to best determine if the current invocation is from CLI or web server?

PHP - how to best determine if the current invocation is from CLI or web server?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Detecting PHP Execution Contexts: CLI vs. Web Server

Determining how a PHP script was invoked is a common requirement when building tools that need to behave differently depending on their environment. For instance, you might want to disable HTML output buffering in a web context but enable verbose error reporting for command-line debugging. The most reliable way to distinguish between a CLI invocation and a web server request (such as Apache with mod_php) is to inspect the $_SERVER['argv'] array or the presence of specific environment variables.

When PHP runs from the command line, it receives arguments directly passed via the shell. This results in the $_SERVER['argv'] variable being populated with an array containing the script name and any following arguments. Conversely, when a web server like Apache invokes PHP via mod_php, it typically does not pass command-line arguments to the script. In these cases, $_SERVER['argv'] will be either an empty array or contain only the script path without meaningful argument data.

A robust detection pattern often involves checking if $_SERVER['argv'][1] exists and contains a non-empty string. If this condition is met, the script is running in CLI mode:

if (!empty($_SERVER['argv'])) {
    // Running from CLI
}

However, relying solely on $_SERVER['argv'] can sometimes be misleading if your application explicitly sets command-line arguments when executed via a web server cron job or similar mechanisms. In such scenarios, the distinction blurs because the execution context is technically CLI-like even though it serves a web request indirectly. To avoid ambiguity in complex deployment architectures, you should also inspect the REQUEST_METHOD variable.

In a standard web server environment running PHP-FPM or mod_php, every HTTP request triggers the generation of an $_SERVER['REQUEST_METHOD'] entry (e.g., GET, POST, HEAD). When executing a script directly from the terminal without any specific arguments that simulate a request, this variable is absent. Therefore, checking for the existence of $_SERVER['REQUEST_METHOD'] provides a strong signal of web server invocation:

if (!empty($_SERVER['REQUEST_METHOD'])) {
    // Running in Web Server context (Apache/Nginx with PHP-FPM/mod_php)
} else {
    // Running in CLI context
}

This logic holds true regardless of the underlying web server, as long as it is generating a standard HTTP request. The combination of checking $_SERVER['argv'] for explicit command-line arguments and verifying the absence or presence of REQUEST_METHOD creates a resilient detection mechanism that handles edge cases where cron jobs might pass arguments while still being part of a web infrastructure.

It is worth noting that using SAPI (Server API) names via php_sapi_name() offers another perspective, though it is less commonly used for simple binary checks. The php_sapi_name() function returns strings like cli or apache2handler. While accurate, this method relies on the SAPI being correctly configured and might not reflect the intended runtime environment in all hybrid setups. For most practical applications involving mod_php or PHP-FPM, checking $_SERVER['REQUEST_METHOD'] remains the most pragmatic approach because it aligns directly with the HTTP request lifecycle that web developers interact with daily.

If your project involves a Laravel application specifically, leveraging the framework's built-in environment detection is often preferable to manual checks. The app()->runningInConsole() method abstracts away these low-level server variable differences and provides a clear boolean indicating whether the application is being executed in CLI mode. This abstraction reduces the risk of introducing subtle bugs when switching between development environments or deploying to different servers.

if (app()->runningInConsole()) {
    // Handle CLI-specific logic
} else {
    // Handle Web-specific logic
}

Using framework-specific methods ensures consistency and avoids reinventing logic that the framework has already solved, making your codebase more maintainable over time.

Our network