You can do this by checking the value of PHP_SAPI
.
If it is "cli"
, it is running on the command line.
Example:
if (PHP_SAPI === 'cli') {
// Está na linha de comando
} else {
// Está rodando no via servidor
}
Note : When the script is running on a server, the values returned by the constant PHP_SAPI
or the php_sapi_name
function may vary depending on the server. For example, when running on Apache2, the returned value will be "apache2handler"
, if you use PHP's built-in server , the returned value is "cli-server"
. But the value returned when using the command line is always "cli"
.