<!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 > echo 5+8; 13 php > function addTwo($n) php > { php { return $n + 2; php { } php > var_dump(addtwo(2)); int(4) php ></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 > strp[TAB][TAB] strpbrk strpos strptime php > 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 > 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 > $fooThisIsAReallyLongVariableName = 42; php > $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 ></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 > #cli.prompt=hello world :> hello world :></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 > #cli.prompt=`echo date('H:i:s');` php > 15:49:35 php > echo 'hi'; hi 15:49:43 php > sleep(2); 15:49:45 php ></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 > #cli.pager=less php > phpinfo(); (output displayed in less) php ></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\> \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>\></em></td> <td> Indicates the prompt character. By default this is <em>></em>, but changes when the shell is inside an unterminated block or string. Possible characters are: <em>' " { ( ></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>