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" } Copy after login |
除此之外,我们还有另一个方法将 PHP 用于外壳脚本。您可以在写一个脚本,并在第一行以 #!/usr/bin/php 开头,在其后加上以 PHP 开始和结尾标记符包含的正常的 PHP 代码,然后为该文件设置正确的运行属性。该方法可以使得该文件能够像外壳脚本或 PERL 脚本一样被直接执行。
#!/usr/bin/php <?php var_dump ($argv); ?> Copy after login <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" } Copy after login 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
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
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!
Related labels:
source:php.cn
Previous article:Detailed explanation of flash sale products using PHP combined with redis
Next article:PHP WeChat generates a WeChat public account QR code and scans it to enter the public account with parameters
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
CommandText property initialization problem
I'm trying to materialize data into 2 (later 3) tables simultaneously using a C# console a...
From 2024-04-04 22:43:24
0
1
385
My multiply (*) command doesn't calculate
My asterisk is not working, when I do the calc() function and do the multiplication notati...
From 2024-04-04 21:18:17
0
1
315
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|