Transferred from: http://www.cnblogs.com/myjavawork/articles/1869205.html
Note: You will need to add the PHP installation directory to the environment variable PATH when you install PHP
(Right-click My Computer, properties, high-level environment variable, if there is a path, add your PHP installation directory to the original path, and if it does not, create a new path)
The following are the options parameters for the command-line mode provided by thePHPbinaries (that is, thephp.exeprogram), which you can query at any time by using the
php-h command.
Usage: php [options] [-f] <file> [args...]
php [options] -r <code> [args...]
php [options] [-- args...]
-s Display colour syntax highlighted source.
-w Display source with stripped comments and whitespace.
-f <file> Parse <file>.
-v Version number
-c <path>|<file> Look for php.ini file in this directory
-a Run interactively
-d foo[=bar] Define INI entry foo with value ‘bar‘
-e Generate extended information for debugger/profiler
-z <file> Load Zend extension <file>.
-l Syntax check only (lint)
-m Show compiled in modules
-i PHP information
-r <code> Run PHP <code> without using script tags <?..?>
-h This help
args... Arguments passed to script. Use -- args when first argument
starts with - or script is read from stdin
|
TheCLI SAPImodule has the following three different ways to get thePHPcode you want to run:
In a Windows environment, use double quotes as much as possible, and use single quotes as much as possible in a Linux environment.
-
-
LetPHPrun the specified file.
php my_script.php php -f "my_script.php"
|
Both of these methods (with or without the-Fparameter) are able to run a givenmy_script.phpfile. You can select any file to run, and thephpscripts You specify do not have to be in. PHPas extensions, they can have arbitrary filenames and extensions.
-
-
Run thePHPcode directly on the command line.
Php-r "Print_r (Get_defined_constants ()); |
When using this method, you should pay attention to the substitution of the shell variables and the use of quotation marks.
Note: Please read the above example carefully, when running the code without the start and end of the marker! With the-rparameter, these tokens are not required, plus they cause syntax errors.
-
-
ProvidesPHPcode that needs to be run through standard input (stdin).
The above usage provides us with a very powerful feature that allows us to dynamically generatePHPcode and run it from the command line, as shown in the example below:
$ some_application | Some_filter | php | Sort-u >final_output.txt |
The above three methods of running code cannot be used at the same time.
Like all shell applications,PHPbinaries (php.exefiles) and theirphpscripts can accept a range of parameters.PHPdoes not limit the number of parameters that are passed to the script (the shell has a limit on the number of characters on the command line, but you typically do not exceed that limit). The parameters passed to your script can be obtained in the global variable$argv. The name of the script whose members are labeled zero in the array is "-" when thePHPcode comes from standard input and is run directly with the-rparameter on the command line. In addition, the global variable$ARGCthe number of member variables in the$argvArray (not the number of parameters passed to the script).
As long as the parameters you send to your script do not begin with a-sign, you do not need to pay too much attention. Sending a parameter to your script with a-start will cause an error, becausePHPwill assume that it should handle these parameters by itself. You can use the parameter list delimiter--to solve this problem. After parsing the parameters inPHP, all the parameters after the symbol will be passed to your script as is.
#The following commands will not run PHP code, but only show instructions for using PHP command line mode:
$ php -r ‘var_dump($argv);‘ -h
Usage: php [options] [-f] <file> [args...]
[...]
#The following command will pass the "- H" parameter to the script program, and PHP will not display the instructions for using the command line mode:
$ php -r "var_dump($argv);" -- -h
Array (2) {
[0]=>
string(1) "-"
[1]=>
string(2) "-h"
}
|
In addition, we have another way to usePHPfor Shell scripting. You can write a script and start with#!/usr/bin/phpin the first line, followed by the normalPHPcode that was included with thePHPstart and end tags, and then set the correct run properties for the file. This method allows the file to be executed directly like a shell script or a PERL script.
#!/usr/bin/php <?php var_dump($argv); ?> |
Assuming that the file name istestand placed in the current directory, we can do the following:
$ chmod 755 test
$ ./test -h -- foo
array(4) {
[0]=>
string(6) "./test"
[1]=>
string(2) "-h"
[2]=>
string(2) "--"
[3]=>
string(3) "foo"
}
|
As you can see, the script will still work correctly when you send the arguments that begin with-to the script.
------------------------------------------------------------------------------Command Options--------------------------------------- --------------
Table 23-3. Command-Line Options
Option Name |
Description |
-S |
Displays the source file with the syntax highlighting color. This parameter uses the built-in mechanism to parse the file and generate anHTML-highlighted version of it and write the results to standard output. Note that this process only generates an<code> [...] </code>HTMLtag block, and does not contain anyHTMLheaders.
Note: This option cannot be used in conjunction with the-rparameter.
|
-W |
Displays the source code with comments and spaces removed.
Note: This option cannot be used in conjunction with the-rparameter.
|
-F |
Parses and runs the given file name. This parameter is optional and can be added without specifying only the file name that needs to be run. |
-V |
Standard output that writes the version information of PHP, PHP SAPI, and Zend. For example:
$ php -v
PHP 4.3.0-dev (cli), Copyright (c) 1997-2002 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies
|
|
-C |
With this parameter, you can specify a directory where thephp.inifile is placed, or specify a custominifile, whose file name may not bephp.ini. For example:
$ php -c /custom/directory/ my_script.php
$ php -c /custom/directory/custom-file.ini my_script.php
|
|
-A |
To run PHP interactively. |
-D |
Use this parameter to set the value of the variable in thephp.inifile itself, with the syntax:
-D Configuration_directive[=value] |
Example:
# Ommiting the value part will set the given configuration directive to "1"
$ php -d max_execution_time
-r ‘$foo = ini_get("max_execution_time"); var_dump($foo);‘
string(1) "1"
# Passing an empty value part will set the configuration directive to ""
php -d max_execution_time=
-r ‘$foo = ini_get("max_execution_time"); var_dump($foo);‘
string(0) ""
# The configuration directive will be set to anything passed after the ‘=‘ character
$ php -d max_execution_time=20
-r ‘$foo = ini_get("max_execution_time"); var_dump($foo);‘
string(2) "20"
$ php
-d max_execution_time=doesntmakesense
-r ‘$foo = ini_get("max_execution_time"); var_dump($foo);‘
string(15) "doesntmakesense"
|
|
-E |
Generates extended information for the debugger, and so on. |
-Z |
Load the Zend extension library. Given only one file name, PHP will attempt to load the extension library from the default path of your system extension library (which is typically specified by/etc/ld.so.confunder the Linux system). If you specify a file name with an absolute path, the default path of the system's extension library will not be used. If the file name is specified with a relative path, PHP only attempts to load the extension library relative to the current directory. |
-l |
. If it fails, theerrors parsing <filename> theand internal parser error messages are written to standard output together with the shell return value set to255. parameter.
Note: This parameter cannot and-ris used together.
|
-M |
Using this parameter, PHP will print out the built-in and loaded PHP and Zend modules:
$ php -m
[PHP Modules]
xml
tokenizer
standard
session
posix
pcre
overload
mysql
mbstring
ctype
[Zend Modules]
|
|
-I. |
The command-line argument calls the phpinfo () function and prints out the result. IfPHPis not working properly, we recommend that you execute the php-i command to see if there is any error message output before or in the information table. Note that the output is inHTMLformat, so the output information is large. |
-R |
Use this parameter to runPHPcode at the command line. You do not need to addPHP's start and end identifiers (<?phpand?>), or you will cause parsing errors.
Note: when using this form ofPHP, you should be careful to avoid conflicting command-line parameter substitutions with the shell environment.
Example of displaying syntax parsing errors
$ php -r "$foo = get_defined_constants();"
Command line code(1) : Parse error - parse error, unexpected ‘=‘
|
The problem here is that the instant use of double quotes", Sh/bash still implements the parameter substitution. Since the$foois not defined, it is replaced with a null character in its place, so at run time, the code actually read byPHPis:
$ php-r "= Get_defined_constants ();" |
The correct way is to use single quotes'. In a string quoted in single quotation marks, the variable is not reverted to its original value by Sh/bash.
$ php -r ‘$foo = get_defined_constants(); var_dump($foo);‘
array(370) {
["E_ERROR"]=>
int(1)
["E_WARNING"]=>
int(2)
["E_PARSE"]=>
int(4)
["E_NOTICE"]=>
int(8)
["E_CORE_ERROR"]=>
[...]
|
If you are using a shell other than Sh/bash, you may encounter other problems. Please report the bug you encountered, or send an email to [email protected].
When you try to introduce an environment variable from a shell to a horse or use a backslash to escape a character, you may encounter a variety of problems, please be careful when you use it!
Note: -Ris valid in the CLI SAPI and is not valid in CGI SAPI.
|
-H |
With this parameter, you can get a complete list of command-line arguments and a simple description of the effects of these parameters. |
PHP's command-line mode allows PHP scripts to be run independently of the WEB server. If you use a Unix system, you need to add a special line of code to the front of your PHP script so that it can be executed so that the system knows what program to run the script with. Under the Windows platform you can associate the double-click properties of thePhp.exeand. phpfiles, or you can write a batch file to execute the script in PHP. The first line of code added to the Unix system does not affect the script running under Windows, so you can also use this method to write a cross-platform scripting program. The following is an example of a simple PHP command-line program.
Example 23-1. PHP script that attempts to run as a command line (script.php)
#!/usr/bin/php <?php if ($ARGC!= 2|| In_array($argv[1], Array ('--help ', '-help ', '-H ', '-? '))) { ?> A command line, PHP script with one option. Usage: <?phpEcho$argv[0]; ?><option> <option> can be some word your would like To print out. With the--help,-help,-H, Or-? Options, you can get the this help. <?php } else { Echo$argv[1]; } ?> |
|
In the above script, we use the first line of special code to indicate that the file should be executed by PHP. We are using the CLI version here, so there will be no HTTP header information output. When you write a command-line application in PHP, you can use two parameters:$argcand$argv. The previous value is an integer that is 1 larger than the number of arguments (the name of the running script itself is also treated as a parameter). The second one contains an array of arguments, the first element of which is the name of the script, and the subscript is the number 0 ($argv [0]).
In the above procedure we have checked whether the number of parameters is greater than 1 or less than 1. The immediate parameters are--help,-help,-Hor-?, and we still print out the Help information and dynamically output the name of the script. If other parameters are received, we also display them.
If you want to run the above script under Unix, you need to make it an executable script and then simply runscript.php echothisorscript.php-h. Under Windows, you can write a batch file for this:
@c:\php\cli\php.exe script.php%1%2%3%4 |
|
Run PHP in the form of a command line