<?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.interactive.php',
    1 => '交互式 shell',
    2 => '交互式 shell',
  ),
  'up' => 
  array (
    0 => 'features.commandline.php',
    1 => 'PHP 的命令行模式',
  ),
  'prev' => 
  array (
    0 => 'features.commandline.io-streams.php',
    1 => 'I/O 流',
  ),
  'next' => 
  array (
    0 => 'features.commandline.webserver.php',
    1 => '内置 Web Server',
  ),
  '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.interactive" class="section">
  <h2 class="title">交互式 shell</h2>

  <p class="para">
   如果 PHP 使用 <strong class="option configure">--with-readline</strong> 选项编译，则 <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> 使用 <strong class="option unknown">-a</strong>
   选项时会提供交互式 shell。自 PHP 7.1.0 起，如果启用了 <a href="book.readline.php" class="link">readline 扩展</a>
   ，交互式 shell 也可以在 Windows 上使用。
  </p>

  <p class="para">
   使用交互式 shell，你可以输入 PHP 代码并直接执行。
  </p>

  <div class="example" id="example-1">
   <p><strong>示例 #1 使用交互式 shell 执行代码</strong></p>
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ php -a
Interactive shell

php &gt; echo 5+8;
13
php &gt; function addTwo($n)
php &gt; {
php { return $n + 2;
php { }
php &gt; var_dump(addtwo(2));
int(4)
php &gt;</pre>
</div>
   </div>

  </div>

  <p class="para">
   交互式 shell 还有对函数、常量、类名、变量、静态方法调用和类常量的 Tab 键补全功能。
  </p>

  <div class="example" id="example-2">
   <p><strong>示例 #2 Tab 键补全</strong></p>
   <div class="example-contents"><p>
    当可能有多个补全时，按两次 Tab 键将会得到补全列表：
   </p></div>
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">php &gt; strp[TAB][TAB]
strpbrk   strpos    strptime  
php &gt; strp</pre>
</div>
   </div>

   <div class="example-contents"><p>
    当仅可能有一个补全，按一次 Tab 键即可在同一行中补全剩余：
   </p></div>
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">php &gt; strpt[TAB]ime(</pre>
</div>
   </div>

   <div class="example-contents"><p>
    补全也适用于当前交互式 shell 会话期间定义的名称：
   </p></div>
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">php &gt; $fooThisIsAReallyLongVariableName = 42;
php &gt; $foo[TAB]ThisIsAReallyLongVariableName</pre>
</div>
   </div>

  </div>

  <p class="simpara">
   交互式 shell 会存储的操作历史记录，可以通过上下键访问。历史记录存储于 <var class="filename">~/.php_history</var>
   文件。自 PHP 8.4.0 起，history 文件的路径可以使用 <var class="envar">PHP_HISTFILE</var> 环境变量设置。
  </p>

  <p class="para">
   <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> 提供了 <code class="parameter">cli.pager</code> 和
   <code class="parameter">cli.prompt</code> 两个 <var class="filename">php.ini</var> 配置。<code class="parameter">cli.pager</code>
   配置允许外部程序（例如 <var class="filename">less</var>）充当分页器输出而不是直接在屏幕上显示。
   <code class="parameter">cli.prompt</code> 配置可以更改 <code class="literal">php &gt;</code> 提示符。
  </p>

  <p class="para">
   在交互式 shell 中还可以使用缩写符号设置 <var class="filename">php.ini</var> 配置。
  </p>

  <div class="example" id="example-3">
   <p><strong>示例 #3 在交互式 shell 中设置 <var class="filename">php.ini</var> 配置</strong></p>
   <div class="example-contents"><p>
    <code class="parameter">cli.prompt</code> 配置：
   </p></div>
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">php &gt; #cli.prompt=hello world :&gt; 
hello world :&gt;</pre>
</div>
   </div>

   <div class="example-contents"><p>
    使用反引号可以在提示中执行 PHP 代码：
   </p></div>
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">php &gt; #cli.prompt=`echo date(&#039;H:i:s&#039;);` php &gt; 
15:49:35 php &gt; echo &#039;hi&#039;;
hi
15:49:43 php &gt; sleep(2);
15:49:45 php &gt;</pre>
</div>
   </div>

   <div class="example-contents"><p>
    设置分页器为 <var class="filename">less</var>：
   </p></div>
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">php &gt; #cli.pager=less
php &gt; phpinfo();
(output displayed in less)
php &gt;</pre>
</div>
   </div>

  </div>

  <p class="para">
   <code class="parameter">cli.prompt</code> 配置支持一些转义字符：
   <table class="doctable table">
    <caption><strong><code class="parameter">cli.prompt</code> 转义字符</strong></caption>
    
     <thead>
      <tr>
       <th>字符</th>
       <th>描述</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><code class="literal">\e</code></td>
       <td>
        用于添加提示符颜色。一个例子是 <code class="literal">\e[032m\v \e[031m\b \e[34m\&gt; \e[0m</code>
       </td>
      </tr>

      <tr>
       <td><code class="literal">\v</code></td>
       <td>PHP 版本。</td>
      </tr>

      <tr>
       <td><code class="literal">\b</code></td>
       <td>
        指明 PHP 所在的块。例如 <code class="literal">/*</code> 表示在多行注释中。外部作用域用 <code class="literal">php</code> 来表示。
       </td>
      </tr>

      <tr>
       <td><code class="literal">\&gt;</code></td>
       <td>
        指明提示字符。默认是 <code class="literal">&gt;</code>，但当 shell 位于未结束的块或者字符串时会发生变化。可能的字符有
        <code class="literal">&#039; &quot; { ( &gt;</code>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>

  <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>

  <div class="section" id="features.commandline.interactive.mode">
   <h2 class="title">交互模式</h2>
   <p class="para">
    如果 readline 扩展不可用，在 PHP 8.1.0 之前，使用 <strong class="option unknown">-a</strong> 选项调用 <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> 会提供交互模式。
    在此模式中，一个完整的 PHP 脚本应该通过 STDIN 给出，并用 <kbd class="keycombo">
     <kbd class="keycap">CTRL</kbd>
     +<kbd class="keycap">D</kbd>
    </kbd>（POSIX）或者 <kbd class="keycombo">
     <kbd class="keycap">CTRL</kbd>
     +<kbd class="keycap">Z</kbd>
    </kbd>
    终止然后 +<kbd class="keycap">ENTER</kbd>（Windows），脚本执行。这与不加 <strong class="option unknown">-a</strong> 选项调用 <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> 基本相同。
   </p>
   <p class="para">
    自 PHP 8.1.0 起，如果 readline 扩展不可用，使用 <strong class="option unknown">-a</strong> 调用 <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> 会失败。
   </p>
  </div>
</div><?php manual_footer($setup); ?>