<span style="font-size: 14px;">Usage: php [options] [-f] <file> [args...]<br> php [options] -r <code> [args...]<br> php [options] [-- args...]<br> -s Display colour syntax highlighted source.<br> -w Display source with stripped comments and whitespace.<br> -f <file> Parse <file>.<br> -v Version number<br> -c <path>|<file> Look for php.ini file in this directory<br> -a Run interactively<br> -d foo[=bar] Define INI entry foo with value 'bar'<br> -e Generate extended information for debugger/profiler<br> -z <file> Load Zend extension <file>.<br> -l Syntax check only (lint)<br> -m Show compiled in modules<br> -i PHP information<br> -r <code> Run PHP <code> without using script tags <?..?><br> -h This help<br> <br> args... Arguments passed to script. Use -- args when first argument <br> starts with - or script is read from stdin<br></span> Copy after login |
## CLI The SAPI module has the following three different methods to obtain your PHP code to be run:
In the Windows environment, try to use double quotes, and in the Linux environment, try to use single quotes.
#Let PHP run the specified file.
Note: Please read the above example carefully, there are no start and end markers when running the code! With the -r parameter, these markers are unnecessary and will cause syntax errors.
The above usage provides us with very powerful functions, allowing us to dynamically generate PHP code and run these codes through the command line as shown in the following example:
Like all shell applications, the PHP binary (php.exe file) and the PHP script it runs can accept a series of parameters. PHP has no limit on the number of arguments passed to a script (the shell has a limit on the number of characters on the command line, but you usually won't exceed that limit). The arguments passed to your script are available in the global variable $argv . The zero-indexed member of this array is the name of the script (when the PHP code comes from standard input and is run directly from the command line with the -r parameter, the name is "-"). In addition, the global variable $argc stores the number of member variables in the $argv array (not the number of parameters passed to the script program).
As long as the parameters you pass to your script do not start with a - symbol, you don't need to pay too much attention. Passing parameters starting with - to your script will cause an error because PHP will think it should handle these parameters itself. You can use the parameter list separator -- to solve this problem. After PHP parses the parameters, all parameters after this symbol will be passed to your script as is.
#!/usr/bin/php<br/><br/> Copy after login Copy after login |
假设改文件名为 test 并被放置在当前目录下,我们可以做如下操作:
$ chmod 755 test<br/>$ ./test -h -- foo<br/>array(4) {<br/> [0]=><br/> string(6) "./test"<br/> [1]=><br/> string(2) "-h"<br/> [2]=><br/> string(2) "--"<br/> [3]=><br/> string(3) "foo"<br/>}<br/> Copy after login |
As you can see, when you pass parameters starting with - to the script, the script still runs normally.
Table 23-3. Command line options
Option Name | Description |
---|---|
##-s | Display source files with syntax highlighting. This parameter uses the built-in mechanism to parse the file and generate an HTML highlighted version of it and write the result to standard output. Please note that all this process does is generate an HTML tag block of
|
Display source code with comments and spaces removed . Note: This option cannot be used together with the -r parameter. | |
## Parses and runs the given filename. This parameter is optional and does not need to be added. It only needs to specify the file name that needs to be run. | |
##Write the version information of PHP, PHP SAPI and Zend standard output. 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 |
Use For this parameter, you can specify a directory where the php.ini file is placed, or directly specify a custom INI file whose file name may not be php.ini. For example: | $ php -c /custom/directory/ my_script.php
$ php -c /custom/directory/custom-file.ini my_script.php ##-a |
Run PHP interactively. | -d |
-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 | Generate extended information for debuggers etc. |
-z | Load the Zend extension library. If only a filename is given, PHP will try to load the extension library from the default path of your system's extension library (on Linux systems, this path is usually specified by /etc/ld.so.conf). If you specify a filename with an absolute path, the system's default path to the extension library will not be used. If you specify a filename with a relative path, PHP will only try to load the extension relative to the current directory. |
##-l | This parameter provides syntax checking for the specified PHP code Convenient method. If successful, the string No syntax errors detected in This parameter will not be able to check for fatal errors (such as undefined functions). If you want to detect fatal errors, please use the -f parameter.
|
Using this parameter, PHP will print out the built-in And the loaded PHP and Zend modules: $ php -m [PHP Modules] xml tokenizer standard session posix pcre overload mysql mbstring ctype [Zend Modules] | |
This command line parameter will call the phpinfo() function and print out result. If PHP is not working properly, we recommend that you execute php -i command to see if there is any error message output before the information table or in the corresponding place. Please note that the output content is in HTML format, so the output information is larger. | |
##Use this parameter to run PHP code on the command line. You do not need to add the PHP starting and ending identifiers (), otherwise it will cause syntax parsing errors. |
|
-h | 使用该参数,您可以得到完整的命令行参数的列表及这些参数作用的简单描述。 |
PHP 的命令行模式能使得 PHP 脚本能完全独立于 WEB 服务器单独运行。如果您使用 Unix 系统,您需要在您的 PHP 脚本的最前面加上一行特殊的代码,使得它能够被执行,这样系统就能知道用什么样的程序要运行该脚本。在 Windows 平台下您可以将 php.exe 和 .php 文件的双击属性相关联,您也可以编写一个批处理文件来用 PHP 执行脚本。为 Unix 系统增加的第一行代码不会影响该脚本在 Windows
下的运行,因此您也可以用该方法编写跨平台的脚本程序。以下是一个简单的PHP 命令行程序的范例。
例子 23-1. 试图以命令行方式运行的 PHP 脚本(script.php) #!/usr/bin/phpThis is a command line PHP script with one option. Usage: |
In the above script, we use the first special line of code to indicate that the file should be executed by PHP. We are using the CLI version here, so no HTTP headers will be output. When you write command line applications in PHP, you can use two parameters: $argc and $argv. The value of the previous one is an integer one greater than the number of parameters (the name of the script being run is also considered a parameter). The second one contains an array of parameters, the first element of which is the name of the script, and the subscript is the number 0 ($argv[0]).
In the above program we checked whether the number of parameters is greater than 1 or less than 1. Even if the parameter is --help, -help, -h or -?, we still print out the help information and dynamically output the name of the script at the same time. 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 run script.php echothis or script.php -h. Under Windows, you can write a batch file for this:
Related recommendations:
Execute based on command line with parameters Method of php script and obtaining parameters
The above is the detailed content of Detailed explanation of PHP command line execution. For more information, please follow other related articles on the PHP Chinese website!