Home >Backend Development >PHP Tutorial >Command line execution under PHP
This article mainly introduces the command line execution under PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
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
CLI SAPI The module has three different ways to get the PHP## you want to run. # Code:
In the Windows environment, try to use double quotes, and in the Linux environment, try to use single quotes.
PHP run the specified file.
-f parameter) are both Able to run the given my_script.php file. You can choose any file to run. The PHP script you specify does not have to have the extension .php. They can have any file name and extension.
PHP code directly on the command line.
##Note:Please read the above example carefully, there is no start when running the code and the end marker! With the -r parameter, these markers are unnecessary and will cause syntax errors.
code that needs to be run through standard input (stdin). The above usage provides us with very powerful functions, allowing us to dynamically generate
PHPcode and run these codes through the command line as shown in the following example:
# 以下命令将不会运行 PHP 代码,而只显示 PHP 命令行模式的使用说明: $ php -r 'var_dump($argv);' -h Usage: php [options] [-f] <file> [args...] [...] # 以下命令将会把“-h”参数传送给脚本程序,PHP 不会显示命令行模式的使用说明: $ php -r "var_dump($argv);" -- -h array(2) { [0]=> string(1) "-" [1]=> string(2) "-h" } |
除此之外,我们还有另一个方法将 PHP 用于外壳脚本。您可以在写一个脚本,并在第一行以 #!/usr/bin/php 开头,在其后加上以 PHP 开始和结尾标记符包含的正常的 PHP 代码,然后为该文件设置正确的运行属性。该方法可以使得该文件能够像外壳脚本或 PERL 脚本一样被直接执行。
#!/usr/bin/php <?php var_dump ($argv); ?> <span style="color:rgb(0,0,0);"><span style="color:rgb(0,0,187);"></span></span>
|
假设改文件名为 test 并被放置在当前目录下,我们可以做如下操作:
$ 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, when you pass parameters starting with - to the script, the script still runs normally.
---------------------------------------- --------------------------------Command options---------- ------------------------------------------
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 a HTML highlighted version of it and write the result to standard output. Please note that all this process does is generate a HTML tag block of ffbe95d20f3893062224282accb13e8f [...] 1cd55414ff5abdfea5dd958e7e547fdd and does not contain any HTML Header.
|
-w |
Displays the source code with comments and spaces removed.
|
-f | Parse and run 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. |
-v |
Write the version information of PHP, PHP SAPI and Zend to the 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 |
With this parameter, you can specify a placement php .ini file directory, 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 |
Use this parameter to set the value of the variable in the php.ini file. The syntax is: : -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 a convenient method for syntax checking of the specified PHP code. If successful, the No syntax errors detected in 2334ac29606bf8a170583e4f7533b1f4 string is written to standard output, and the shell returns a value of 0. If it fails, Errors parsing 2334ac29606bf8a170583e4f7533b1f4 will be written to standard output along with the internal parser error message, and the shell return value will be set to 255. 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.
|
-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 | This command line parameter will call the phpinfo() function and print out the result. If PHP is not working properly, we recommend that you execute the php -i command to see if there are any error messages 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. |
-r |
Use this parameter to run PHP code on the command line. You don't include the starting and ending identifiers of PHP (a96219156e02bc2a284f7ff939b47305), otherwise it will cause syntax parsing errors.
|
Using this parameter, you can get the complete list of command line parameters and A brief description of what these parameters do. |
PHP’s command line mode enables PHP scripts to run completely independently of the WEB server. If you are using a Unix system, you need to add a special line of code at the beginning of your PHP script to enable it to be executed, so that the system knows what program to use to run the script. Under the Windows platform, you can associate the double-click attributes of the
php.exe and .php files, or you can write a batch file to execute the script using PHP. The first line of code added for Unix systems will not affect the running of the script under Windows, so you can also use this method to write cross-platform scripts. The following is an example of a simple PHP command line program.
Example 23-1. PHP script (script.php) trying to run in command line mode#!/usr/bin/php8deb48bca3d1183fcb518f448274ad98This is a command line PHP script with one option. Usage: 0e8116fbeca0753b81d15e8da712a426 5a07473c87748fb1bf73f23d45547ab8 5a07473c87748fb1bf73f23d45547ab8 can be some word you would like to print out. With the --help, -help, -h, or -? options, you can get this help.1368379bf0e06ed88222aa51004d0324 |
|
The above is the detailed content of Command line execution under PHP. For more information, please follow other related articles on the PHP Chinese website!