Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 1886

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Interactive shell</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="features.commandline.io-streams.html">I/O streams</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="features.commandline.webserver.html">Built-in web server</a></div>
 <div class="up"><a href="features.commandline.html">Command line usage</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="features.commandline.interactive" class="section">
  <h2 class="title">Interactive shell</h2>

  <p class="para">
   As of PHP 5.1.0, the <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> 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.
  </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-378">
   <p><strong>Example #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-379">
   <p><strong>Example #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="para">
   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.
  </p>

  <p class="para">
   As of PHP 5.4.0, the <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> provides
   the <var class="filename">php.ini</var> settings <em><code class="parameter">cli.pager</code></em> and
   <em><code class="parameter">cli.prompt</code></em>. The <em><code class="parameter">cli.pager</code></em>
   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 <em><code class="parameter">cli.prompt</code></em> setting makes it possible to
   change the <em>php &gt;</em> prompt.
  </p>

  <p class="para">
   In PHP 5.4.0 it was also made possible to set
   <var class="filename">php.ini</var> settings in the interactive shell using a shorthand notation.
  </p>

  <div class="example" id="example-380">
   <p><strong>Example #3 Setting <var class="filename">php.ini</var> settings in the interactive shell</strong></p>
   <div class="example-contents"><p>
    The <em><code class="parameter">cli.prompt</code></em> 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 <em><code class="parameter">cli.prompt</code></em> setting supports a few escape
   sequences:
   <table class="doctable table">
    <caption><strong><em><code class="parameter">cli.prompt</code></em> escape sequences</strong></caption>
    
     <thead>
      <tr>
       <th>Sequence</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><em>\e</em></td>
       <td>
        Used for adding colors to the prompt. An example could be
        <em>\e[032m\v \e[031m\b \e[34m\&gt; \e[0m</em>
       </td>
      </tr>

      <tr>
       <td><em>\v</em></td>
       <td>The PHP version.</td>
      </tr>

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

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

     </tbody>
    
   </table>

  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    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.
   </p>
  </p></blockquote>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    <a href="language.oop5.autoload.html" class="link">Autoloading</a> is not
    available if using PHP in <acronym title="Command Line Interpreter/Interface">CLI</acronym> interactive mode.
   </p>
  </p></blockquote>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="features.commandline.io-streams.html">I/O streams</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="features.commandline.webserver.html">Built-in web server</a></div>
 <div class="up"><a href="features.commandline.html">Command line usage</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>