⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 features.commandline.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 4 页
字号:
     <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Our&nbsp;simple&nbsp;test&nbsp;application&nbsp;named&nbsp;test.php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">getcwd</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</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] &lt;file&gt; [--] [args...]       php [options] -r &lt;code&gt; [--] [args...]       php [options] [-B &lt;begin_code&gt;] -R &lt;code&gt; [-E &lt;end_code&gt;] [--] [args...]       php [options] [-B &lt;begin_code&gt;] -F &lt;file&gt; [-E &lt;end_code&gt;] [--] [args...]       php [options] -- [args...]       php [options] -a  -a               Run interactively  -c &lt;path&gt;|&lt;file&gt; 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 &#039;bar&#039;  -e               Generate extended information for debugger/profiler  -f &lt;file&gt;        Parse and execute &lt;file&gt;.  -h               This help  -i               PHP information  -l               Syntax check only (lint)  -m               Show compiled in modules  -r &lt;code&gt;        Run PHP &lt;code&gt; without using script tags &lt;?..?&gt;  -B &lt;begin_code&gt;  Run PHP &lt;begin_code&gt; before processing input lines  -R &lt;code&gt;        Run PHP &lt;code&gt; for every input line  -F &lt;file&gt;        Parse and execute &lt;file&gt; for every input line  -E &lt;end_code&gt;    Run PHP &lt;end_code&gt; 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 &lt;file&gt;        Load Zend extension &lt;file&gt;.  args...          Arguments passed to script. Use -- args when first argument                   starts with - or script is read from stdin  --ini            Show configuration file names  --rf &lt;name&gt;      Show information about function &lt;name&gt;.  --rc &lt;name&gt;      Show information about class &lt;name&gt;.  --re &lt;name&gt;      Show information about extension &lt;name&gt;.  --ri &lt;name&gt;      Show configuration for extension &lt;name&gt;.</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 &#039;print_r(get_defined_constants());&#039;</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 &gt;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&#039;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&#039;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 &#039;var_dump($argv);&#039; -hUsage: php [options] [-f] &lt;file&gt; [args...][...]# This will pass the &#039;-h&#039; argument to your script and prevent PHP from showing it&#039;s usage$ php -r &#039;var_dump($argv);&#039; -- -harray(2) {  [0]=&gt;  string(1) &quot;-&quot;  [1]=&gt;  string(2) &quot;-h&quot;}</pre></div>  </pre></div> </div> <p class="para">  However, there&#039;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">&lt;?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">?&gt;</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]=&gt;  string(6) &quot;./test&quot;  [1]=&gt;  string(2) &quot;-h&quot;  [2]=&gt;  string(2) &quot;--&quot;  [3]=&gt;  string(3) &quot;foo&quot;}</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&#039;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&#039;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 + -