<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/features.commandline.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'features.commandline.options.php',
    1 => '选项',
    2 => '命令行选项',
  ),
  'up' => 
  array (
    0 => 'features.commandline.php',
    1 => 'PHP 的命令行模式',
  ),
  'prev' => 
  array (
    0 => 'features.commandline.differences.php',
    1 => '和其它 SAPI 模块的区别',
  ),
  'next' => 
  array (
    0 => 'features.commandline.usage.php',
    1 => '用法',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'features/commandline.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="features.commandline.options" class="section">
  <h2 class="title">命令行选项</h2>
  
  
  <p class="para">
   PHP 二进制文件可以随时通过执行带 <strong class="option unknown">-h</strong> 参数的 PHP 命令获取提供的命令行选项列表：
   <div class="example-contents screen">
<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 &lt;addr&gt;:&lt;port&gt; Run with built-in web server.
  -t &lt;docroot&gt;     Specify document root &lt;docroot&gt; for built-in web server.
  -s               Output HTML syntax highlighted source.
  -v               Version number
  -w               Output 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;.
  --rz &lt;name&gt;      Show information about Zend extension &lt;name&gt;.
  --ri &lt;name&gt;      Show configuration for extension &lt;name&gt;.
</pre></div>
   </div>
  </p>
  
  <p class="para">
   <table class="doctable table">
    <caption><strong>命令行选项</strong></caption>
    
     <thead>
      <tr>
       <th>选项</th>
       <th>长选项</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>-a</td>
       <td>--interactive</td>
       <td>
        <p class="para">
         运行交互式 PHP。参阅<a href="features.commandline.interactive.php" class="link">交互式 shell</a>
         章节获取更多信息。
        </p>
       </td>
      </tr>

      <tr>
       <td>-b</td>
       <td>--bindpath</td>
       <td>
        <p class="para">
         外部 FASTCGI 服务器模式绑定路径（仅 <abbr title="Common Gateway Interface">CGI</abbr> 可用）。
        </p>
       </td>
      </tr>

      <tr>
       <td>-C</td>
       <td>--no-chdir</td>
       <td>
        <p class="para">
         不改变脚本的目录（仅 <abbr title="Common Gateway Interface">CGI</abbr> 可用）。
        </p>
       </td>
      </tr>

      <tr>
       <td>-q</td>
       <td>--no-header</td>
       <td>
        <p class="para">
         安静模式。禁止输出 <abbr title="Hypertext Transfer Protocol">HTTP</abbr>
         头（仅 <abbr title="Common Gateway Interface">CGI</abbr> 可用）。
        </p>
       </td>
      </tr>

      <tr>
       <td>-T</td>
       <td>--timing</td>
       <td>
        <p class="para">
         测量脚本重复 <var class="varname">count</var> 次的执行时间（仅 <abbr title="Common Gateway Interface">CGI</abbr> 可用）。
        </p>
       </td>
      </tr>

      <tr>
       <td>-c</td>
       <td>--php-ini</td>
       <td>
        <p class="para">
         在指定目录查找 <var class="filename">php.ini</var> 或者自定义 <code class="literal">INI</code>
         文件（名称可以不是 <var class="filename">php.ini</var>），例如：
        </p>
        <p class="para"><div class="informalexample">
         <div class="example-contents screen">
<div class="cdata"><pre>
$ php -c /custom/directory/ my_script.php

$ php -c /custom/directory/custom-file.ini my_script.php
</pre></div>
         </div>
        </div></p>
        <p class="para">
         如果未指定此选项，<var class="filename">php.ini</var> 将在<a href="configuration.file.php" class="link">默认位置</a>搜索。
        </p>
       </td>
      </tr>

      <tr>
       <td>-n</td>
       <td>--no-php-ini</td>
       <td>
        <p class="para">
         完全忽略 <var class="filename">php.ini</var>。
        </p>
       </td>
      </tr>

      <tr>
       <td>-d</td>
       <td>--define</td>
       <td>
        <p class="para">
         允许设置 <var class="filename">php.ini</var> 中配置指令的值。语法是：
         <div class="example-contents screen">
 <div class="cdata"><pre>
 -d configuration_directive[=value]
 </pre></div>
         </div>
        </p>
        <p class="para"><div class="example" id="example-1">
         <p><strong>示例 #1 使用 <code class="literal">-d</code> 设置 INI 配置的示例</strong></p>
         <div class="example-contents screen">
<div class="cdata"><pre>
# 忽略值部分，将会设置配置指令为 &quot;1&quot;
$ php -d max_execution_time
        -r &#039;$foo = ini_get(&quot;max_execution_time&quot;); var_dump($foo);&#039;
string(1) &quot;1&quot;

# 传递空值，将会设置配置指令为 &quot;&quot;
php -d max_execution_time=
        -r &#039;$foo = ini_get(&quot;max_execution_time&quot;); var_dump($foo);&#039;
string(0) &quot;&quot;

# 配置指令将会设置为 &#039;=&#039; 字符之后传递的任何值
$  php -d max_execution_time=20
        -r &#039;$foo = ini_get(&quot;max_execution_time&quot;); var_dump($foo);&#039;
string(2) &quot;20&quot;
$  php
        -d max_execution_time=doesntmakesense
        -r &#039;$foo = ini_get(&quot;max_execution_time&quot;); var_dump($foo);&#039;
string(15) &quot;doesntmakesense&quot;
</pre></div>
         </div>
        </div></p>
       </td>
      </tr>

      <tr>
       <td>-e</td>
       <td>--profile-info</td>
       <td>
        <p class="para">
         激活扩展信息模式，用于调试/分析。
        </p>
       </td>
      </tr>

      <tr>
       <td>-f</td>
       <td>--file</td>
       <td>
        <p class="para">
         解析并执行指定文件。 <strong class="option unknown">-f</strong> 
         可选且可以忽略 —— 只需提供需要执行的文件名就足够。
        </p>
       </td>
      </tr>

      <tr>
       <td>-h and -?</td>
       <td>--help and --usage</td>
       <td>
        输出命令行选项列表并描述了这些选项的作用。
       </td>
      </tr>

      <tr>
       <td>-i</td>
       <td>--info</td>
       <td>
        调用 <span class="function"><a href="function.phpinfo.php" class="function">phpinfo()</a></span> 并输出结果。如果 PHP 
        不能正常工作，建议使用命令 <strong class="command">php -i</strong>
        查看在信息表输出之前或者中间某个位置是否有错误消息。注意当使用
        <abbr title="Common Gateway Interface">CGI</abbr> 模式时会输出 <abbr title="Hyper Text Markup Language">HTML</abbr>，
        这会非常大。
       </td>
      </tr>

      <tr>
       <td>-l</td>
       <td>--syntax-check</td>
       <td>
        <p class="para">
         检查语法但不执行指定 PHP 代码。如果未指定文件，则将处理来自标准输入的输入，否则将检查每个文件。成功时，文本
         <code class="literal">No syntax errors detected in &lt;filename&gt;</code> 将写入标准输出。失败时，除了内部解析器错误外，文本
         <code class="literal">Errors parsing &lt;filename&gt;</code> 将写入标准输出。如果在指定文件（或标准输入）中发现任何错误，则将
         shell 返回代码设置为 <code class="literal">-1</code>，否则将 shell 返回代码设置为 <code class="literal">0</code>。
        </p>
        <p class="para">
         此选项不会找到需要执行代码的致命错误（如未定义的函数）。
        </p>
        <blockquote class="note"><p><strong class="note">注意</strong>: 
         <p class="para">
          在 PHP 8.3.0 之前，只能指定一个要检查的文件。
         </p>
        </p></blockquote>
        <blockquote class="note"><p><strong class="note">注意</strong>: 
         <p class="para">
          此选项不能和 <strong class="option unknown">-r</strong> 一起使用。
         </p>
        </p></blockquote>
       </td>
      </tr>

      <tr>
       <td>-m</td>
       <td>--modules</td>
       <td>
        <p class="para"><div class="example" id="example-2">
         <p><strong>示例 #2 打印内置（且已加载的） PHP 和 Zend 模块</strong></p>
         <div class="example-contents screen">
<div class="cdata"><pre>
$ php -m
[PHP Modules]
xml
tokenizer
standard
session
posix
pcre
overload
mysql
mbstring
ctype

[Zend Modules]
</pre></div>
         </div>
        </div></p>
       </td>
      </tr>

      <tr>
       <td>-r</td>
       <td>--run</td>
       <td>
        <p class="para">
         允许在命令行内直接执行单行 PHP 代码。
         <strong>不需要</strong>加上 PHP 开始和结束标识符（<code class="literal">&lt;?php</code> 和
         <code class="literal">?&gt;</code>），否则将会造成语法错误。
        </p>
        <blockquote class="note"><p><strong class="note">注意</strong>: 
         <p class="para">
          使用这种形式的 PHP 时，应注意避免和 shell 环境进行的命令行参数替换相冲突。
         </p>
         <div class="example" id="example-3">
          <p><strong>示例 #3 当使用双引号时会造成语法错误</strong></p>
          <div class="example-contents screen">
<div class="cdata"><pre>
$ php -r &quot;$foo = get_defined_constants();&quot;
PHP Parse error:  syntax error, unexpected &#039;=&#039; in Command line code on line 1

Parse error: syntax error, unexpected &#039;=&#039; in Command line code on line 1
</pre></div>
          </div>
         </div>
         <p class="para">
          这里的问题是在使用双引号 <code class="literal">&quot;</code> 时 sh/bash 会执行变量替换。
          由于 <var class="varname">$foo</var> 变量没有被定义，因此它将替换为空字符，
          从而导致传递给 PHP 执行的代码实际上会变成：
         </p>
         <div class="informalexample">
          <div class="example-contents screen">
<div class="cdata"><pre>
$ php -r &quot; = get_defined_constants();&quot;
</pre></div>
          </div>
         </div>

         <p class="para">
          正确的方式是使用单引号 <code class="literal">&#039;</code>
          。使用单引号时 sh/bash 不会进行变量替换。
         </p>
         <div class="example" id="example-4">
          <p><strong>示例 #4 使用单引号防止 shell 变量替换</strong></p>
          <div class="example-contents screen">
<div class="cdata"><pre>
$ php -r &#039;$foo = get_defined_constants(); var_dump($foo);&#039;
array(370) {
  [&quot;E_ERROR&quot;]=&gt;
  int(1)
  [&quot;E_WARNING&quot;]=&gt;
  int(2)
  [&quot;E_PARSE&quot;]=&gt;
  int(4)
  [&quot;E_NOTICE&quot;]=&gt;
  int(8)
  [&quot;E_CORE_ERROR&quot;]=&gt;
  [...]
</pre></div>
          </div>
         </div>
         <p class="para">
          如果使用的 shell 不是 sh/bash，可能会碰到更多的问题 —— 
          如果有的话，请向 <a href="https://github.com/php/php-src/issues" class="link external">&raquo;&nbsp;https://github.com/php/php-src/issues</a>
          报告 bug。试图在命令行中使用（ shell/PHP 
          ）变量或者使用反斜杠转义时需要格外小心，因为很容易遇到问题。勿谓言之不预！
         </p>
        </p></blockquote>
        <blockquote class="note"><p><strong class="note">注意</strong>: 
         <p class="para">
          可以在 <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> 中使用 
          <strong class="option unknown">-r</strong>，但不能在 <em>CGI</em> <abbr title="Server Application Programming Interface">SAPI</abbr> 中使用。
         </p>
        </p></blockquote>
        <blockquote class="note"><p><strong class="note">注意</strong>: 
         <p class="para">
          此选项仅适用于非常基本的代码，因此在此模式下会忽略某些配置指令（如 <a href="ini.core.php#ini.auto-prepend-file" class="link">auto_prepend_file</a> 和 <a href="ini.core.php#ini.auto-append-file" class="link">auto_append_file</a>）。
         </p>
        </p></blockquote>
       </td>
      </tr>

      <tr>
       <td>-B</td>
       <td>--process-begin</td>
       <td>
        <p class="para">
         处理 stdin 之前需要执行的 PHP 代码。
        </p>
       </td>
      </tr>

      <tr>
       <td>-R</td>
       <td>--process-code</td>
       <td>
        <p class="para">
         对每个输入行都执行的 PHP 代码。
        </p>
        <p class="para">
         此模式下有两个特殊变量：
         <var class="varname">$argn</var> 和 <var class="varname">$argi</var>。
         <var class="varname">$argn</var> 将包含 PHP 正在处理的行，
          <var class="varname">$argi</var> 将包含正在处理的行号。
        </p>
       </td>
      </tr>

      <tr>
       <td>-F</td>
       <td>--process-file</td>
       <td>
        <p class="para">
         对每个输入行都执行的 PHP 文件。
        </p>
       </td>
      </tr>

      <tr>
       <td>-E</td>
       <td>--process-end</td>
       <td>
        <p class="para">
         在处理完输入后执行的 PHP 代码。
        </p>
        <p class="para"><div class="example" id="example-5">
         <p><strong>示例 #5 使用 <strong class="option unknown">-B</strong> 、 <strong class="option unknown">-R</strong> 、 <strong class="option unknown">-E</strong> 
          选项统计项目总行数。
         </strong></p>
         <div class="example-contents screen">
<div class="cdata"><pre>
$ find my_proj | php -B &#039;$l=0;&#039; -R &#039;$l += count(@file($argn));&#039; -E &#039;echo &quot;Total Lines: $l\n&quot;;&#039;
Total Lines: 37328
</pre></div>
         </div>
        </div></p>
       </td>
      </tr>

      <tr>
       <td>-S</td>
       <td>--server</td>
       <td>
        <p class="para">
         启动 <a href="features.commandline.webserver.php" class="link">内置 web 服务器</a>.
        </p>
       </td>
      </tr>

      <tr>
       <td>-t</td>
       <td>--docroot</td>
       <td>
        为<a href="features.commandline.webserver.php" class="link">内置 web 服务器</a>指定文档根目录。
       </td>
      </tr>

      <tr>
       <td>-s</td>
       <td>--syntax-highlight 和 --syntax-highlighting</td>
       <td>
        <p class="para">
         为源代码添加语法高亮显示。
        </p>
        <p class="para">
         此选项将使用内部机制解析文件并将生成的 HTML 高亮版本写入到标准输出。
         注意它所做的只是生成一块 <code class="literal">&lt;code&gt; [...] &lt;/code&gt;</code>
         HTML 标记，没有 HTML header。
        </p>
        <blockquote class="note"><p><strong class="note">注意</strong>: 
         <p class="para">
          此选项不能和 <strong class="option unknown">-r</strong> 一起使用。
         </p>
        </p></blockquote>
       </td>
      </tr>

      <tr>
       <td>-v</td>
       <td>--version</td>
       <td>
        <p class="para"><div class="example" id="example-6">
         <p><strong>示例 #6 使用 <strong class="option unknown">-v</strong> 获取 <abbr title="Server Application Programming Interface">SAPI</abbr>
         的名称以及 PHP 和 Zend 的版本号</strong></p>
         <div class="example-contents screen">
<div class="cdata"><pre>
$ php -v
PHP 5.3.1 (cli) (built: Dec 11 2009 19:55:07)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
</pre></div>
         </div>
        </div></p>
       </td>
      </tr>

      <tr>
       <td>-w</td>
       <td>--strip</td>
       <td>
        <p class="para">
         显示忽略注释和空格后的源代码。
        </p>
        <blockquote class="note"><p><strong class="note">注意</strong>: 
         <p class="para">
          此选项不能和 <strong class="option unknown">-r</strong> 一起使用。
         </p>
        </p></blockquote>
       </td>
      </tr>

      <tr>
       <td>-z</td>
       <td>--zend-extension</td>
       <td>
        <p class="para">
         加载 Zend 扩展。如果仅指定了文件名，PHP 将尝试从当前系统默认函数库中尝试加载此扩展
         （例如在 Linux 上通常是 <var class="filename">/etc/ld.so.conf</var>）。
         传递绝对路径的文件名将不会使用系统库搜索路径。如果用相对路径指定的文件名，则 PHP 
         仅试图在当前目录的相对目录加载扩展库。
        </p>
       </td>
      </tr>

      <tr>
       <td class="empty">&nbsp;</td>
       <td>--ini</td>
       <td>
        <p class="para">
         展示配置文件名和扫描目录。
         <div class="example" id="example-7">
          <p><strong>示例 #7 <code class="literal">--ini</code> 示例</strong></p>
          <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ php --ini
Configuration File (php.ini) Path: /usr/dev/php/5.2/lib
Loaded Configuration File:         /usr/dev/php/5.2/lib/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)</pre>
</div>
          </div>

         </div>
        </p>
       </td>
      </tr>

      <tr>
       <td>--rf</td>
       <td>--rfunction</td>
       <td>
        <p class="para">
         展示指定函数或者类方法的有关信息（例如参数名称和数量）。
        </p>
        <p class="para">
         如果 PHP 在编译时启用 <a href="book.reflection.php" class="link">Reflection</a>
         支持，该选项才可以使用。
        </p>
        <p class="para">
         <div class="example" id="example-8">
          <p><strong>示例 #8 基础 <code class="literal">--rf</code> 用法</strong></p>
          <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ php --rf var_dump
Function [ &lt;internal&gt; public function var_dump ] {

  - Parameters [2] {
    Parameter #0 [ &lt;required&gt; $var ]
    Parameter #1 [ &lt;optional&gt; $... ]
  }
}</pre>
</div>
          </div>

         </div>
        </p>
       </td>
      </tr>

      <tr>
       <td>--rc</td>
       <td>--rclass</td>
       <td>
        <p class="para">
         展示指定类的有关信息（常量、属性和方法的列表）。
        </p>
        <p class="para">
         如果 PHP 在编译时启用 <a href="book.reflection.php" class="link">Reflection</a>
         支持，该选项才可以使用。
        </p>
        <p class="para">
         <div class="example" id="example-9">
          <p><strong>示例 #9 <code class="literal">--rc</code> 示例</strong></p>
          <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ php --rc Directory
Class [ &lt;internal:standard&gt; class Directory ] {

  - Constants [0] {
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [0] {
  }

  - Methods [3] {
    Method [ &lt;internal&gt; public method close ] {
    }

    Method [ &lt;internal&gt; public method rewind ] {
    }

    Method [ &lt;internal&gt; public method read ] {
    }
  }
}</pre>
</div>
          </div>

         </div>
        </p>
       </td>
      </tr>

      <tr>
       <td>--re</td>
       <td>--rextension</td>
       <td>
        <p class="para">
         展示指定扩展的有关信息（<var class="filename">php.ini</var> 选项、定义函数、常量和类的列表）。
        </p>
        <p class="para">
         如果 PHP 在编译时启用 <a href="book.reflection.php" class="link">Reflection</a>
         支持，该选项才可以使用。
        </p>
        <p class="para">
         <div class="example" id="example-10">
          <p><strong>示例 #10 <code class="literal">--re</code> 示例</strong></p>
          <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ php --re json
Extension [ &lt;persistent&gt; extension #19 json version 1.2.1 ] {

  - Functions {
    Function [ &lt;internal&gt; function json_encode ] {
    }
    Function [ &lt;internal&gt; function json_decode ] {
    }
  }
}</pre>
</div>
          </div>

         </div>
        </p>
       </td>
      </tr>

      <tr>
       <td>--rz</td>
       <td>--rzendextension</td>
       <td>
        <p class="para">
         展示指定 Zend 扩展的配置信息（也可以通过 <span class="function"><a href="function.phpinfo.php" class="function">phpinfo()</a></span> 返回相同信息）。
        </p>
       </td>
      </tr>

      <tr>
       <td>--ri</td>
       <td>--rextinfo</td>
       <td>
        <p class="para">
         展示指定扩展的配置信息（也可以通过 <span class="function"><a href="function.phpinfo.php" class="function">phpinfo()</a></span> 返回相同信息）。
         使用 “main” 作为扩展名可以获取到核心配置信息。
        </p>
        <p class="para">
         <div class="example" id="example-11">
          <p><strong>示例 #11 <code class="literal">--ri</code> 示例</strong></p>
          <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ php --ri date

date

date/time support =&gt; enabled
&quot;Olson&quot; Timezone Database Version =&gt; 2009.20
Timezone Database =&gt; internal
Default timezone =&gt; Europe/Oslo

Directive =&gt; Local Value =&gt; Master Value
date.timezone =&gt; Europe/Oslo =&gt; Europe/Oslo
date.default_latitude =&gt; 59.930972 =&gt; 59.930972
date.default_longitude =&gt; 10.776699 =&gt; 10.776699
date.sunset_zenith =&gt; 90.583333 =&gt; 90.583333
date.sunrise_zenith =&gt; 90.583333 =&gt; 90.583333</pre>
</div>
          </div>

         </div>
        </p>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    选项 <code class="literal">-rBRFEH</code>、 <code class="literal">--ini</code> 、
    <code class="literal">--r[fcezi]</code> 仅可以在 <abbr title="Command Line Interpreter/Interface">CLI</abbr> 中使用。
   </p>
  </p></blockquote>
 </div><?php manual_footer($setup); ?>