-1
php -f script.php param1 param2

At the moment, I am just checking whether isset($argv). Is this the best way?

P.S. I also wanted to know if all input parameters are always stored in $argv?

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
NoNameZ
  • 785
  • 4
  • 14
  • 22
  • Did you even try searching before asking your question? [see accepted answer here](http://stackoverflow.com/questions/933367/php-how-to-best-determine-if-the-current-invocation-is-from-cli-or-web-server) – RobMasters Sep 18 '12 at 10:35
  • yes, $argv contains all CLI arguments – confiq Sep 18 '12 at 11:00

2 Answers2

1

As you can read here Is there any way to know if a php script is running in cli mode? you can use this function:

function is_cli()
{
    return php_sapi_name() === 'cli';
}
Community
  • 1
  • 1
Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64
0

Check whether or not a REQUEST_METHOD is set:

/**
 * Check if the site is being visited (in a browser) or run as a program from the 
 * commandline.
 * @return boolean true if the request appears to come from the WWW, false if not.
 */
function is_web_request () {
    return isset($_SERVER['REQUEST_METHOD']);
}
user268396
  • 11,576
  • 2
  • 31
  • 26