All PHP distributions, whether compiled from source code or pre-created versions, are provided with a PHP executable by default. This executable can be used to run the command-line PHP program.
To find this executable file on your system, follow the steps below:
Windows: Placed in the main PHP installation directory, the file name is Php.exe or (in the old version of PHP) is Php-cli.exe.
Linux: Saved in the bin/subdirectory of the PHP installation directory.
It is important to note that the CLI mode and CGI mode runtime php.ini are not the same set of configurations and need to be configured separately.
Regardless of the operating system, you need to test it to make sure it works correctly by calling it with the-v parameter:
shell> /path/php.exe -v PHP 5.0.0 (cli) (built: Jun 1 2005 18:32:10) Copyright (c) 1997-2004 The PHP Group Zend Engine v2.0.0, Copyright (c) 1998-2004 Zend Technologies |
It should return the version number of PHP.
Using CLI commands
A simple PHP CLI program, named hello.php
<?php echo "Hello from the CLI" ; ?> |
Now, try running the program at a command-line prompt by invoking the CLI executable and providing the script's file name:
shell> /path/php.exe /example/hello.php Hello from the CLI |
Use of standard inputs and outputs
The PHP CLI defines three constants to make it easier to interact with the interpreter at the command-line prompt. These constants are shown in the following table
Constant |
Description |
Stdin |
Standard input Devices |
STDOUT |
Standard output devices |
STDERR |
Standard error Device |
You can use these three constants in your PHP scripts to accept user input, or to display the results of processing and calculations.
Examples of Use:
<?php // ask for input fwrite(STDOUT, "Enter your name: " ); // get input $name = trim( fgets (STDIN)); // write input back fwrite(STDOUT, "Hello, $name!" ); ?> |
Output
D:\>\wamp\bin\php\php5.3.0\php.exe \tools\index.php Enter your name: kkk Hello, kkk! |
In this script, the fwrite () function first writes a message to the standard output device asking for the user's name. It then reads the user input information obtained from the standard input device into a PHP variable and merges it into a string. The string is then printed out to the standard output device using fwrite ().
command Line custom variable 1 "$argv | $ARGC"
It is common practice to enter program parameters at the command line to change how they are run. You can also do this to the CLI program.
The PHP CLI has two special variables that are specifically designed to achieve this purpose:
One is $argv variable, which saves the parameters passed to the PHP script as a separate array element through the command line;
The other is $ARGC variable, which holds the number of elements in the $ARGV array.
Examples of Use:
Output
D:\>\wamp\bin\php\php5.3.0\php.exe \tools\index.php bac ddd Array ( [0] => \tools\index.php [1] => bac [2] => ddd ) |
As you can see from the output, the value passed to the index.php is automatically shown as an array element in the $ARGV. Note that the first argument $argv is always the script's own name.
Note: We can also use console_getopt the Pear class adds more complex command-line arguments to PHP.
command line custom variable 2 "Receive parameters using console_getopt "
Note: This variable is only available when register_argc_argv is open
Getopt ($option, $longopts)///First $option receives-H vb second parameter receives--require SSS
Usage examples
<?php
$shortopts =
""
;
$shortopts .=
"f:"
;
// Required value
$shortopts .=
"v::"
;
// Optional value
$shortopts .=
"abc"
;
// These options do not accept values
$longopts =
array
(
"required:"
,
// Required value
"optional::"
,
// Optional value
"option"
,
// No value
"opt"
,
// No value
);
$options =
getopt
(
$shortopts
,
$longopts
);
var_dump(
$options
);
?>
|
Ouput:
D:\>\wamp\bin\php\php5.3.0\php.exe \tools\index.php -f
"value for f" -v -a --re
quired value --optional=
"optional value" --option will
array
(6) {
[
"f"
]=>
string(11)
"value for f"
[
"v"
]=>
bool(false)
[
"a"
]=>
bool(false)
[
"required"
]=>
string(5)
"value"
[
"optional"
]=>
string(14)
"optional value"
[
"option"
]=>
bool(false)
}
|
command-line variable 3 "using CLI parameters"
In addition to using the command line to pass PHP script parameters, you can also pass the PHP CLI parameter to change how it works.
Parameters |
Description |
-A |
Running run interactively interactively |
-C |
Path reads PHP's. ini file from path |
-N |
Run directly without reading PHP's. ini file |
-M |
List the compiled modules |
-I. |
Display information about PHP builds |
-L |
Check the syntax of the PHP script |
-S |
Display the source code in a colored fashion |
-W |
Display the source code after removing the comment |
-H |
Show Help |
Interactive mode
You can also interactively use the PHP CLI, which is the input command, to get results immediately.
To get this effect, you just need to invoke the CLI executable with one parameter, just like this:
shell> /path/to/php -a Interactive mode enabled <?php echo mktime (); 1121187283 echo 2+2; 4 exit (); shell> |
Alternatively, you can invoke the CLI executable without using the-a parameter and enter the complete script or code snippet directly.
Use <ctrl>-d to end the code snippet and let the CLI execute it. See the following example:
shell> /path/to/php
<?php
echo
date
(
"d-M-Y h:i:s"
, time());
?>
<Ctrl-D>
12-Jul-2005 06:54:04
PHP CLI Commands custom parameter passing