Executing PHP filesThere are three different ways of supplying the CLI SAPI with PHP code to be executed:
As with every shell application, the PHP binary accepts a number of
arguments; however, the PHP script can also receive further arguments. The
number of arguments that can be passed to your script is not limited by PHP
(and although the shell has a limit to the number of characters which can be
passed, this is not in general likely to be hit). The arguments passed to
the script are available in the global array $argv. The
first index (zero) always contains the name of the script as called from the
command line. Note that, if the code is executed in-line using the command
line switch -r, the value of $argv[0]
will be A second global variable, $argc, contains the number of elements in the $argv array (not the number of arguments passed to the script).
As long as the arguments to be passed to the script do not start with
the # This will not execute the given code but will show the PHP usage $ php -r 'var_dump($argv);' -h Usage: php [options] [-f] <file> [args...] [...] # This will pass the '-h' argument to the script and prevent PHP from showing its usage $ php -r 'var_dump($argv);' -- -h array(2) { [0]=> string(1) "-" [1]=> string(2) "-h" }
However, on Unix systems there's another way of using PHP for shell
scripting: make the first line of the script start with
Example #1 Execute PHP script as shell script
Assuming this file is named test in the current directory, it is now possible to do the following: $ chmod +x test $ ./test -h -- foo array(4) { [0]=> string(6) "./test" [1]=> string(2) "-h" [2]=> string(2) "--" [3]=> string(3) "foo" }
As can be seen, in this case no special care needs to be taken when passing parameters
starting with
The PHP executable can be used to run PHP scripts absolutely independent of
the web server. On Unix systems, the special
Example #2 Script intended to be run from command line (script.php)
The script above includes the Unix shebang first line to indicate that this file should be run by PHP. We are working with a CLI version here, so no HTTP headers will be output. The program first checks that there is the required one argument (in addition to the script name, which is also counted). If not, or if the argument was --help, -help, -h or -?, the help message is printed out, using $argv[0] to dynamically print the script name as typed on the command line. Otherwise, the argument is echoed out exactly as received. To run the above script on Unix, it must be made executable, and called simply as script.php echothis or script.php -h. On Windows, a batch file similar to the following can be created for this task:
Example #3 Batch file to run a command line PHP script (script.bat) @echo OFF "C:\php\php.exe" script.php %* Assuming the above program is named script.php, and the CLI php.exe is in C:\php\php.exe, this batch file will run it, passing on all appended options: script.bat echothis or script.bat -h. See also the Readline extension documentation for more functions which can be used to enhance command line applications in PHP.
On Windows, PHP can be configured to run without the need to
supply the C:\php\php.exe or the
|