📄 features.commandline.html
字号:
<div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Our simple test application named test.php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">getcwd</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <p class="para"> When using the <i>CGI</i> version, the output is: </p> <div class="example-contents"><pre><div class="cdata"><pre>$ pwd/tmp$ php -q another_directory/test.php/tmp/another_directory</pre></div> </pre></div> <p class="para"> This clearly shows that PHP changes its current directory to the one of the executed script. </p> <p class="para"> Using the <i>CLI SAPI</i> yields: </p> <div class="example-contents"><pre><div class="cdata"><pre>$ pwd/tmp$ php -f another_directory/test.php/tmp</pre></div> </pre></div> <p class="para"> This allows greater flexibility when writing shell tools in PHP. </p> </div> <blockquote><p><b class="note">Note</b>: The <i>CGI SAPI</i> supports this <i>CLI SAPI</i> behaviour by means of the <span class="option">-C</span> switch when run from the command line. <br /> </p></blockquote> </li> </ul> </p> <p class="para"> The list of command line options provided by the PHP binary can be queried anytime by running PHP with the <span class="option">-h</span> switch: <div class="example-contents"><pre><div class="cdata"><pre>Usage: php [options] [-f] <file> [--] [args...] php [options] -r <code> [--] [args...] php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...] php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...] php [options] -- [args...] php [options] -a -a Run interactively -c <path>|<file> Look for php.ini file in this directory -n No php.ini file will be used -d foo[=bar] Define INI entry foo with value 'bar' -e Generate extended information for debugger/profiler -f <file> Parse and execute <file>. -h This help -i PHP information -l Syntax check only (lint) -m Show compiled in modules -r <code> Run PHP <code> without using script tags <?..?> -B <begin_code> Run PHP <begin_code> before processing input lines -R <code> Run PHP <code> for every input line -F <file> Parse and execute <file> for every input line -E <end_code> Run PHP <end_code> after processing all input lines -H Hide any passed arguments from external tools. -s Display colour syntax highlighted source. -v Version number -w Display source with stripped comments and whitespace. -z <file> Load Zend extension <file>. args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdin --ini Show configuration file names --rf <name> Show information about function <name>. --rc <name> Show information about class <name>. --re <name> Show information about extension <name>. --ri <name> Show configuration for extension <name>.</pre></div> </pre></div> </p> <p class="para"> The <i>CLI SAPI</i> has three different ways of getting the PHP code you want to execute: <ol class="orderedlist"> <li class="listitem"> <p class="para"> Telling PHP to execute a certain file. </p> <div class="informalexample"> <div class="example-contents"><pre><div class="cdata"><pre>php my_script.phpphp -f my_script.php</pre></div> </pre></div> </div> <p class="para"> Both ways (whether using the <span class="option">-f</span> switch or not) execute the file <var class="filename">my_script.php</var>. You can choose any file to execute - your PHP scripts do not have to end with the <i>.php</i> extension but can have any name or extension you wish. </p> <blockquote><p><b class="note">Note</b>: If you need to pass arguments to your scripts you need to pass <i>--</i> as the first argument when using the <span class="option">-f</span> switch. <br /> </p></blockquote> </li> <li class="listitem"> <p class="para"> Pass the PHP code to execute directly on the command line. </p> <div class="informalexample"> <div class="example-contents"><pre><div class="cdata"><pre>php -r 'print_r(get_defined_constants());'</pre></div> </pre></div> </div> <p class="para"> Special care has to be taken in regards of shell variable substitution and quoting usage. </p> <blockquote><p><b class="note">Note</b>: Read the example carefully, there are no beginning or ending tags! The <span class="option">-r</span> switch simply does not need them. Using them will lead to a parser error. <br /> </p></blockquote> </li> <li class="listitem"> <p class="para"> Provide the PHP code to execute via standard input (<i>stdin</i>). </p> <p class="para"> This gives the powerful ability to dynamically create PHP code and feed it to the binary, as shown in this (fictional) example: </p> <div class="informalexample"> <div class="example-contents"><pre><div class="cdata"><pre>$ some_application | some_filter | php | sort -u >final_output.txt</pre></div> </pre></div> </div> </li> </ol> You cannot combine any of the three ways to execute code. </p> <p class="para"> Like every shell application, the PHP binary accepts a number of arguments but your PHP script can also receive arguments. The number of arguments which can be passed to your script is not limited by PHP (the shell has a certain size limit in the number of characters which can be passed; usually you won't hit this limit). The arguments passed to your script are available in the global array <var class="varname"><a href="reserved.variables.argv.html" class="classname">$argv</a></var>. The zero index always contains the script name (which is <i>-</i> in case the PHP code is coming from either standard input or from the command line switch <span class="option">-r</span>). The second registered global variable is <var class="varname"><a href="reserved.variables.argc.html" class="classname">$argc</a></var> which contains the number of elements in the <var class="varname"><a href="reserved.variables.argv.html" class="classname">$argv</a></var> array (<em class="emphasis">not</em> the number of arguments passed to the script). </p> <p class="para"> As long as the arguments you want to pass to your script do not start with the <i>-</i> character, there's nothing special to watch out for. Passing an argument to your script which starts with a <i>-</i> will cause trouble because PHP itself thinks it has to handle it. To prevent this, use the argument list separator <i>--</i>. After this separator has been parsed by PHP, every argument following it is passed untouched to your script. </p> <div class="informalexample"> <div class="example-contents"><pre><div class="cdata"><pre># This will not execute the given code but will show the PHP usage$ php -r 'var_dump($argv);' -hUsage: php [options] [-f] <file> [args...][...]# This will pass the '-h' argument to your script and prevent PHP from showing it's usage$ php -r 'var_dump($argv);' -- -harray(2) { [0]=> string(1) "-" [1]=> string(2) "-h"}</pre></div> </pre></div> </div> <p class="para"> However, there's another way of using PHP for shell scripting. You can write a script where the first line starts with <i>#!/usr/bin/php</i>. Following this you can place normal PHP code included within the PHP starting and end tags. Once you have set the execution attributes of the file appropriately (e.g. <strong class="command">chmod +x test</strong>) your script can be executed like a normal shell or perl script: </p> <div class="example"> <p><b>Example #1 Execute PHP script as shell script</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000">#!/usr/bin/php<br /><span style="color: #0000BB"><?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$argv</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> Assuming this file is named <var class="filename">test</var> in the current directory, we can now do the following: </p></div> <div class="example-contents"><pre><div class="cdata"><pre>$ chmod +x test$ ./test -h -- fooarray(4) { [0]=> string(6) "./test" [1]=> string(2) "-h" [2]=> string(2) "--" [3]=> string(3) "foo"}</pre></div> </pre></div> </div> <p class="para"> As you see, in this case no care needs to be taken when passing parameters which start with <i>-</i> to your script. </p> <p class="para"> Long options are available since PHP 4.3.3. <table border="5"> <caption><b>Command line options</b></caption> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Option</th> <th colspan="1">Long Option</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">-a</td> <td colspan="1" rowspan="1" align="left">--interactive</td> <td colspan="1" rowspan="1" align="left"> <p class="para"> Runs PHP interactively. If you compile PHP with the <a href="ref.readline.html" class="link">Readline</a> extension (which is not available on Windows), you'll have a nice shell, including a completion feature (e.g. you can start typing a variable name, hit the TAB key and PHP completes its name) and a typing history that can be accessed using the arrow keys. The history is saved in the <var class="filename">~/.php_history</var> file. </p> <blockquote><p><b class="note">Note</b>: Files included through <a href="ini.core.html#ini.auto-prepend-file" class="link">auto_prepend_file</a> and <a href="ini.core.html#ini.auto-append-file" class="link">auto_append_file</a> are parsed in this mode but with some restrictions - e.g. functions have to be defined before called. <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: <a href="language.oop5.autoload.html" class="link">Autoloading</a> is not available if using PHP in CLI interactive mode. <br /> </p></blockquote> </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">-c</td> <td colspan="1" rowspan="1" align="left">--php-ini</td> <td colspan="1" rowspan="1" align="left"> <p class="para"> This option can either specify a directory where to look for <var class="filename">php.ini</var> or specify a custom <i>INI</i> file (which does not need to be named <var class="filename">php.ini</var>), e.g.: </p> <p class="para"><div class="informalexample"> <div class="example-contents"><pre><div class="cdata"><pre>$ php -c /custom/directory/ my_script.php$ php -c /custom/directory/custom-file.ini my_script.php</pre></div> </pre></div> </div></p> <p class="para"> If you don't specify this option, file is searched in <a href="configuration.html#configuration.file" class="link">default locations</a>. </p> </td> </tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -