The CLI SAPI has three different ways of
getting the PHP code you want to execute:
Telling PHP to execute a certain file.
php my_script.php
php -f my_script.php
Both ways (whether using the -f switch or not) execute the
file my_script.php. You can choose any file to execute -
your PHP scripts do not have to end with the .php extension
but can have any name or extension you wish.
Pass the PHP code to execute directly on the command line.
php -r 'print_r(get_defined_constants());'
Special care has to be taken in regards of shell variable
substitution and quoting usage.
Note: Read the example carefully, there are no beginning
or ending tags! The -r switch simply does not need them.
Using them will lead to a parser error.
Provide the PHP code to execute via standard input (stdin).
This gives the powerful ability to dynamically create PHP
code and feed it to the binary, as shown in this (fictional)
example: $ some_application | some_filter | php | sort -u
>final_output.txt
You cannot combine any of the three ways to execute code.
Like every shell application, the PHP binary accepts a
number of arguments but your PHP script can also receive
arguments. The number of arguments which can be passed to
your script is not limited by PHP (the shell has a certain
size limit in the number of characters which can be passed;
usually you won't hit this limit). The arguments passed
to your script are available in the global array $argv.
The zero index always contains the script name (which is
- in case the PHP code is coming from either standard input
or from the command line switch -r). The second registered
global variable is $argc which contains the number of elements
in the $argv array (not the number of arguments passed to
the script).
|