<?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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'features.commandline.interactive.php',
    1 => 'Interactive shell',
    2 => 'Interactive shell',
  ),
  'up' => 
  array (
    0 => 'features.commandline.php',
    1 => 'Command line usage',
  ),
  'prev' => 
  array (
    0 => 'features.commandline.io-streams.php',
    1 => 'I/O streams',
  ),
  'next' => 
  array (
    0 => 'features.commandline.webserver.php',
    1 => 'Built-in web server',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    '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">Interactive shell</h2>

  <p class="para">
   The <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> provides an interactive shell using the
   <strong class="option unknown">-a</strong> option if PHP is compiled with the <strong class="option configure">--with-readline</strong> option.
   As of PHP 7.1.0 the interactive shell is also available on Windows, if the
   <a href="book.readline.php" class="link">readline extension</a> is enabled.
  </p>

  <p class="para">
   Using the interactive shell you are able to type PHP code and have it
   executed directly.
  </p>

  <div class="example" id="example-1">
   <p><strong>Приклад #1 Executing code using the interactive 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">
   The interactive shell also features tab completion for functions,
   constants, class names, variables, static method calls and class
   constants.
  </p>

  <div class="example" id="example-2">
   <p><strong>Приклад #2 Tab completion</strong></p>
   <div class="example-contents"><p>
    Pressing the tab key twice when there are multiple possible completions
    will result in a list of these completions:
   </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>
    When there is only one possible completion, pressing tab once will
    complete the rest on the same line:
   </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>
    Completion will also work for names that have been defined
    during the current interactive shell session:
   </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">
   The interactive shell stores your history which can be accessed using the up
   and down keys. The history is saved in the
   <var class="filename">~/.php_history</var> file.
   As of PHP 8.4.0, the path to the history file can be set using the
   <var class="envar">PHP_HISTFILE</var> environment variable.
  </p>

  <p class="para">
   The <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> provides
   the <var class="filename">php.ini</var> settings <code class="parameter">cli.pager</code> and
   <code class="parameter">cli.prompt</code>. The <code class="parameter">cli.pager</code>
   setting allows an external program (such as <var class="filename">less</var>) to
   act as a pager for the output instead of being displayed directly on the
   screen. The <code class="parameter">cli.prompt</code> setting makes it possible to
   change the <code class="literal">php &gt;</code> prompt.
  </p>

  <p class="para">
   It is also possible to set
   <var class="filename">php.ini</var> settings in the interactive shell using a shorthand notation.
  </p>

  <div class="example" id="example-3">
   <p><strong>Приклад #3 Setting <var class="filename">php.ini</var> settings in the interactive shell</strong></p>
   <div class="example-contents"><p>
    The <code class="parameter">cli.prompt</code> setting:
   </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>
    Using backticks it is possible to have PHP code executed in the prompt:
   </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>
    Setting the pager to <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">
   The <code class="parameter">cli.prompt</code> setting supports a few escape
   sequences:
   <table class="doctable table">
    <caption><strong><code class="parameter">cli.prompt</code> escape sequences</strong></caption>
    
     <thead>
      <tr>
       <th>Sequence</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><code class="literal">\e</code></td>
       <td>
        Used for adding colors to the prompt. An example could be
        <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>The PHP version.</td>
      </tr>

      <tr>
       <td><code class="literal">\b</code></td>
       <td>
        Indicates which block PHP is in. For instance <code class="literal">/*</code> to
        indicate being inside a multi-line comment. The outer scope is denoted by
        <code class="literal">php</code>.
       </td>
      </tr>

      <tr>
       <td><code class="literal">\&gt;</code></td>
       <td>
        Indicates the prompt character. By default this is
        <code class="literal">&gt;</code>, but changes when the shell is inside an
        unterminated block or string. Possible characters are: <code class="literal">&#039; &quot; {
        ( &gt;</code>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>

  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    Files included through <a href="ini.core.php#ini.auto-prepend-file" class="link">auto_prepend_file</a> and <a href="ini.core.php#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.
   </p>
  </p></blockquote>

  <div class="section" id="features.commandline.interactive.mode">
   <h2 class="title">Interactive mode</h2>
   <p class="para">
    If the readline extension is not available, prior to PHP 8.1.0, invoking the <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> with the
    <strong class="option unknown">-a</strong> option provided the interactive mode. In this mode, a
    complete PHP script is supposed to be given via STDIN, and after termination
    with
    <kbd class="keycombo">
     <kbd class="keycap">CTRL</kbd>
     +<kbd class="keycap">D</kbd>
    </kbd>
    (POSIX) or
    <kbd class="keycombo">
     <kbd class="keycap">CTRL</kbd>
     +<kbd class="keycap">Z</kbd>
    </kbd>
    followed by +<kbd class="keycap">ENTER</kbd> (Windows), this script is evaluated.
    This is basically the same as invoking the <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> without the <strong class="option unknown">-a</strong>
    option.
   </p>
   <p class="para">
    As of PHP 8.1.0, invoking the <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> with the <strong class="option unknown">-a</strong> option
    fails, if the readline extension is not available.
   </p>
  </div>
 </div><?php manual_footer($setup); ?>