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

📄 features.commandline.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Using PHP from the command line</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.html">Features</a></div> <div class="next" style="text-align: right; float: right;"><a href="funcref.html">Function Reference</a></div> <div class="up"><a href="features.html">Features</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Using PHP from the command line</h1> <p class="para">  As of version 4.3.0, PHP supports a new  <i>SAPI</i> type (Server Application Programming Interface)  named <i>CLI</i> which means <em class="emphasis">Command Line  Interface</em>. As the name implies, this <i>SAPI</i> type  main focus is on developing shell (or desktop as well) applications with  PHP.  There are quite a few differences between the  <i>CLI SAPI</i> and other <i>SAPI</i>s which are  explained in this chapter.  It&#039;s worth mentioning  that <i>CLI</i> and <i>CGI</i> are different  SAPI&#039;s although they do share many of the same behaviors. </p> <p class="para">  The <i>CLI SAPI</i> was released for the first time with  PHP 4.2.0, but was still experimental and had  to be explicitly enabled with <span class="option">--enable-cli</span> when running  <strong class="command">./configure</strong>.  Since PHP 4.3.0 the  <i>CLI SAPI</i> is no longer experimental and the option  <span class="option">--enable-cli</span> is on by default.  You may use  <span class="option">--disable-cli</span> to disable it. </p> <p class="para">  As of PHP 4.3.0, the name, location and existence of the CLI/CGI binaries  will differ depending on how PHP is installed on your system.  By default  when executing <strong class="command">make</strong>, both the CGI and CLI are built and  placed as <var class="filename">sapi/cgi/php</var> and <var class="filename">sapi/cli/php</var>   respectively, in your PHP source directory.  You will note that both are  named php. What happens during <strong class="command">make install</strong> depends on  your configure line.  If a module SAPI is chosen during configure, such as apxs, or the  <span class="option">--disable-cgi</span> option is used, the CLI is copied to  <var class="filename">{PREFIX}/bin/php</var> during <strong class="command">make install</strong>  otherwise the CGI is placed there.  So, for example, if <span class="option">--with--apxs  </span> is in your configure line then the CLI is copied to  <var class="filename">{PREFIX}/bin/php</var> during <strong class="command">make  install</strong>. If you want to override the installation of the CGI  binary, use <strong class="command">make install-cli</strong> after <strong class="command">make  install</strong>.  Alternatively you can specify <span class="option">  --disable-cgi</span> in your configure line. </p> <blockquote><p><b class="note">Note</b>:      Because both <span class="option">--enable-cli</span> and   <span class="option">--enable-cgi</span> are enabled by default,   simply having <span class="option">--enable-cli</span> in your   configure line does not necessarily mean the CLI will be copied as   <var class="filename">{PREFIX}/bin/php</var> during <strong class="command">make install</strong>.  <br /> </p></blockquote> <p class="para">  The Windows packages between PHP 4.2.0 and PHP 4.2.3 distributed the CLI as  <var class="filename">php-cli.exe</var>, living in the same folder as the CGI  <var class="filename">php.exe</var>. Starting with PHP 4.3.0 the Windows package  distributes the CLI as <var class="filename">php.exe</var> in a separate folder  named <var class="filename">cli</var>, so <var class="filename">cli/php.exe  </var>. Starting with PHP 5, the CLI is distributed in the main folder,  named  <var class="filename">php.exe</var>. The CGI version is distributed as  <var class="filename">php-cgi.exe</var>. </p> <p class="para">  As of PHP 5, a new <var class="filename">php-win.exe</var> file is distributed.  This is equal to the CLI version, except that php-win doesn&#039;t output  anything and thus provides no console (no &quot;dos box&quot; appears on the screen).  This  behavior is similar to php-gtk. You should configure with  <span class="option">--enable-cli-win32</span>. </p> <blockquote><p><b class="note">Note</b>:   <b>What SAPI do I have?</b><br />     From a shell, typing <strong class="command">php -v</strong> will tell you   whether <var class="filename">php</var> is CGI or CLI.  See also the function   <a href="function.php-sapi-name.html" class="function">php_sapi_name()</a> and the constant <b><tt>   PHP_SAPI</tt></b>.  <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>:      A Unix <i>man</i>ual page was added in PHP 4.3.2.  You may    view this by typing <strong class="command">man php</strong> in your shell environment.  <br /> </p></blockquote> <p class="para">  Remarkable differences of the <i>CLI SAPI</i> compared to other  <i>SAPI</i>s:  <ul class="itemizedlist">   <li class="listitem">    <p class="para">     Unlike the <i>CGI SAPI</i>, no headers are written to the     output.    </p>    <p class="para">     Though the <i>CGI SAPI</i> provides a way to suppress HTTP     headers, there&#039;s no equivalent switch to enable them in the <i>CLI     SAPI</i>.    </p>    <p class="para">     CLI is started up in quiet mode by default, though the <span class="option">-q</span>     and <span class="option">--no-header</span> switches are kept for compatibility so     that you can use older CGI scripts.    </p>    <p class="para">     It does not change the working directory to that of the script.     (<span class="option">-C</span> and <span class="option">--no-chdir</span> switches kept for     compatibility)    </p>    <p class="para">     Plain text error messages (no HTML formatting).    </p>   </li>   <li class="listitem">    <p class="para">     There are certain <var class="filename">php.ini</var> directives which are overridden by the <i>CLI     SAPI</i> because they do not make sense in shell environments:    </p>    <p class="para">     <table border="5">      <caption><b>Overridden <var class="filename">php.ini</var> directives</b></caption>      <colgroup>       <thead valign="middle">        <tr valign="middle">         <th colspan="1">Directive</th>         <th colspan="1"><i>CLI SAPI</i> default value</th>         <th colspan="1">Comment</th>        </tr>       </thead>       <tbody valign="middle" class="tbody">        <tr valign="middle">         <td colspan="1" rowspan="1" align="left"><a href="errorfunc.configuration.html#ini.html-errors" class="link">html_errors</a></td>         <td colspan="1" rowspan="1" align="left"><b><tt>FALSE</tt></b></td>         <td colspan="1" rowspan="1" align="left">          It can be quite hard to read the error message in your shell when          it&#039;s cluttered with all those meaningless <i>HTML</i>          tags, therefore this directive defaults to <b><tt>FALSE</tt></b>.         </td>        </tr>        <tr valign="middle">         <td colspan="1" rowspan="1" align="left"><a href="outcontrol.configuration.html#ini.implicit-flush" class="link">implicit_flush</a></td>         <td colspan="1" rowspan="1" align="left"><b><tt>TRUE</tt></b></td>         <td colspan="1" rowspan="1" align="left">          It is desired that any output coming from          <a href="function.print.html" class="function">print()</a>, <a href="function.echo.html" class="function">echo()</a> and friends is          immediately written to the output and not cached in any buffer. You          still can use <a href="ref.outcontrol.html" class="link">output buffering</a>          if you want to defer or manipulate standard output.         </td>        </tr>        <tr valign="middle">         <td colspan="1" rowspan="1" align="left"><a href="info.configuration.html#ini.max-execution-time" class="link">max_execution_time</a></td>         <td colspan="1" rowspan="1" align="left">0 (unlimited)</td>         <td colspan="1" rowspan="1" align="left">          Due to endless possibilities of using PHP in          shell environments, the maximum execution time has been set to          unlimited. Whereas applications written for the web are often          executed very quickly, shell application tend to have a much          longer execution time.         </td>        </tr>        <tr valign="middle">         <td colspan="1" rowspan="1" align="left"><a href="ini.core.html#ini.register-argc-argv" class="link">register_argc_argv</a></td>         <td colspan="1" rowspan="1" align="left"><b><tt>TRUE</tt></b></td>         <td colspan="1" rowspan="1" align="left">         <p class="para">          Because this setting is <b><tt>TRUE</tt></b> you will always have access to          <em class="emphasis">argc</em> (number of arguments passed to the           application) and <em class="emphasis">argv</em> (array of the actual          arguments) in the <i>CLI SAPI</i>.         </p>         <p class="para">          As of PHP 4.3.0, the PHP variables <var class="varname"><a href="reserved.variables.argc.html" class="classname">$argc</a></var>          and <var class="varname"><a href="reserved.variables.argv.html" class="classname">$argv</a></var> are registered and filled in with the appropriate           values when using the <i>CLI SAPI</i>. Prior to this version,          the creation of these variables behaved as they do in          <i>CGI</i> and <i>MODULE</i> versions          which requires the PHP directive          <a href="ini.core.html#ini.register-globals" class="link">register_globals</a> to          be <em class="emphasis">on</em>.  Regardless of version or register_globals          setting, you can always go through either           <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER</a></var> or           <var class="varname">$HTTP_SERVER_VARS</var>.  Example:          <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['argv']</a></var>         </p>         </td>        </tr>       </tbody>      </colgroup>     </table>    </p>    <blockquote><p><b class="note">Note</b>:            These directives cannot be initialized with another value from the      configuration file <var class="filename">php.ini</var> or a custom one (if specified). This is a      limitation because those default values are applied after all      configuration files have been parsed. However, their value can be changed      during runtime (which does not make sense for all of those directives,      e.g. <a href="ini.core.html#ini.register-argc-argv" class="link">register_argc_argv</a>).     <br />    </p></blockquote>   </li>   <li class="listitem">    <p class="para">     To ease working in the shell environment, the following constants     are defined:     <table border="5">      <caption><b>CLI specific Constants</b></caption>      <colgroup>       <thead valign="middle">        <tr valign="middle">         <th colspan="1">Constant</th>         <th colspan="1">Description</th>        </tr>       </thead>       <tbody valign="middle" class="tbody">        <tr valign="middle">         <td colspan="1" rowspan="1" align="left"><b><tt>STDIN</tt></b></td>         <td colspan="1" rowspan="1" align="left">          <p class="para">An already opened stream to <i>stdin</i>. This saves         opening it with         <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$stdin&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'php://stdin'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>         </div>         If you want to read single line from <i>stdin</i>, you can         use         <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$line&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">STDIN</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;reads&nbsp;one&nbsp;line&nbsp;from&nbsp;STDIN<br /></span><span style="color: #0000BB">fscanf</span><span style="color: #007700">(</span><span style="color: #0000BB">STDIN</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"%d\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;reads&nbsp;number&nbsp;from&nbsp;STDIN<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>         </div>         </p></td>        </tr>        <tr valign="middle">         <td colspan="1" rowspan="1" align="left"><b><tt>STDOUT</tt></b></td>         <td colspan="1" rowspan="1" align="left"><p class="para">         An already opened stream to <i>stdout</i>. This saves         opening it with         <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$stdout&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'php://stdout'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>         </div>         </p></td>        </tr>        <tr valign="middle">         <td colspan="1" rowspan="1" align="left"><b><tt>STDERR</tt></b></td>         <td colspan="1" rowspan="1" align="left">          <p class="para">           An already opened stream to <i>stderr</i>.           This saves opening it with           <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$stderr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'php://stderr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>           </div>          </p>         </td>        </tr>       </tbody>      </colgroup>     </table>    </p>    <p class="para">     Given the above, you don&#039;t need to open e.g. a stream for     <i>stderr</i> yourself but simply use the constant instead of     the stream resource:     <div class="example-contents"><div class="cdata"><pre>php -r &#039;fwrite(STDERR, &quot;stderr\n&quot;);&#039;</pre></div>     </div>     You do not need to explicitly close these streams, as they are closed     automatically by PHP when your script ends.    </p>    <blockquote><p><b class="note">Note</b>:            These constants are not available in case of reading PHP script from      <i>stdin</i>.     <br />    </p></blockquote>   </li>   <li class="listitem">    <p class="para">     The <i>CLI SAPI</i> does <em class="emphasis">not</em> change the current directory to the directory     of the executed script!    </p>    <div class="informalexample">     <p class="para">      Example showing the difference to the <i>CGI SAPI</i>:     </p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -