Developing applications designed to run from a terminal instead of through a Web browser requires us, as developers, to look a bit differently at the way scripts are written. For instance, when developing shell scripts in PHP, you do not need to be concerned with things such as sessions (the "stateless" protocol problem does not apply to client-side scripts). However, new problems arise when you attempt to design scripts that require interaction with the user because we no longer can rely on the $_GET, $_POST, and $_COOKIE superglobals for input or HTML for output. Instead of these facilities, to interact with the user, your scripts must instead rely on CLI-specific extensions and third-party applications.
Command-Line Arguments and Return Codes
One
of the first new ways of accepting input from the user in CLI scripts
is to use command-line arguments. These arguments are passed to your
scripts when they are executed and generally enable or define certain
options. An example of a command-line argument is the -h argument
passed to the PHP executable (which displayed a list of all available
argumentssee the previous section). To receive a list of command-line
arguments in PHP, you'll need to be familiar with two predefined
variables: $argc and $argv. These two variables (abbreviations for argument count and argument values)
store all the information passed to your scripts in the form of
command-line arguments. As you may have already guessed, the $argc
parameter is an integer count of the total number of arguments passed,
and $argv is an integer array of the value(s) of those arguments in the
order they were passed. Every CLI PHP script that is executed will
always have at least one argument provided to it. This argument
represents the filename of the script currently being executed.