Consola interactiva

Desde PHP 5.1.0, SAPI CLI ofrece una consola interactiva si se usa con el modificador -a y PHP está compilado con la opción --with-readline .

Al usar la consola interactiva, se puede escribir directamente código PHP que se ejecuta al momento.

Ejemplo #1 Ejecutando código desde la consola interactiva

$ php -a
Interactive shell

php > echo 5+8;
php > function addTwo($n)
php > {
php { return $n + 2;
php { }
php > var_dump(addtwo(2));
php >

La consola interactiva, además, proporciona autocompletado mediante el tabulador de funciones, constantes, nombres de clases, variables, llamadas a métodos estáticos y constantes de clases.

Ejemplo #2 Autocompletado con el tabulador

Al pulsar dos veces la tecla tabulador habiendo múltiples opciones de completados, se mostrará una lista con éstas:

php > strp[TAB][TAB]
strpbrk   strpos    strptime  
php > strp

Cuando sólo hay una posible opción, sólo con pulsar una vez el tabulador se completará el resto de la línea:

php > strpt[TAB]ime(

También funciona el autocompletado para nombres que se han definido durante la sesión de consola interactiva:

php > $fooEsteEsUnNombreDeVariableMuyLargo = 42;
php > $foo[TAB]EsteEsUnNombreDeVariableMuyLargo

La consola interactiva almacena tu historial, al que se puede acceder usando las teclas arriba y abajo. El historial se almacena en el fichero ~/.php_history.

Ya en PHP 5.4.0, la SAPI CLI provee las configuraciones de php.ini, cli.pager y cli.prompt. La configuración de cli.pager permite a un programa externo (tal como less) para que funcione como un paginador para la salida en lugar de se desplegado directamente en la pantalla. Las configuraciones de cli.prompt permite cambiar el indicador de ingreso de órdenes php >.

In PHP 5.4.0 también fue posible establecer las configuraciones de php.ini en la shell interactiva utilizando una notación abreviada.

Ejemplo #3 Estableciendo configuraciones de php.ini en la shell interactiva

La configuración de cli.prompt:

php > #cli.prompt=hola mundo :> 
  hola mundo :>

Usando comillas simples inclinadas es posible ejecutar código PHP en el indicador de órdenes:

php > #cli.prompt=`echo date('H:i:s');` php > 
  15:49:35 php > echo 'hola';
  15:49:43 php > sleep(2);
  15:49:45 php >

Establecer el paginador a less:

php > #cli.pager=less
  php > phpinfo();
  (salida desplegada en less)
  php >

La configuración de cli.prompt soporta unas cuantas secuencias de escape:

Secuencias de escape de cli.prompt
Sequence: Description:
\e Utilizado para agregar colores al ingreso de órdenes. Un ejemplo podría ser \e[032m\v \e[031m\b \e[34m\> \e[0m
\v La versión de PHP.
\b Indica cual bloque de PHP está dentro. Por ejemplo /* se usa para indicar que está dentro de un comentario multilineal. El alcance externo es denotado por php.
\> Indica el caracter de ingreso de órdenes. El caracter predeterminado es >, pero cambia cuando la shell está dentro de un bloque indeterminado o una cadena. Los caracteres posibles son: ' " { ( >


Los ficheros que se han incluido en este modo mediante auto_prepend_file y auto_append_file se analizan con algunas restricciones - p.ej. las funciones deben estar definidas antes de que se carguen.


La auto-carga no está disponible al usar PHP en modo interactivo en CLI.

add a note add a note

User Contributed Notes 10 notes

Ryan P
5 years ago
Interactive Shell and Interactive Mode are not the same thing, despite the similar names and functionality.

If you type 'php -a' and get a response of 'Interactive Shell' followed by a 'php>' prompt, you have interactive shell available (PHP was compiled with readline support). If instead you get a response of 'Interactive mode enabled', you DO NOT have interactive shell available and this article does not apply to you.

You can also check 'php -m' and see if readline is listed in the output - if not, you don't have interactive shell.

Interactive mode is essentially like running php with stdin as the file input. You just type code, and when you're done (Ctrl-D), php executes whatever you typed as if it were a normal PHP (PHTML) file - hence you start in interactive mode with '<?php' in order to execute code.

Interactive shell evaluates every expression as you complete it (with ; or }), reports errors without terminating execution, and supports standard shell functionality via readline (history, tab completion, etc). It'
s an enhanced version of interactive mode that is ONLY available if you have the required libraries, and is an actual PHP shell that interprets everything you type as PHP code - using '<?php' will cause a parse error.

Finally, if
you're running on Windows, you're probably screwed. From what I'm seeing in other comments here, you don't have readline, and without readline there is no interactive shell.
spencer at aninternetpresence dot net
6 years ago
In Windows, press Enter after your ending PHP tag and then hit Ctrl-Z to denote the end-of-file:

C:\>php -a
Interactive mode enabled

echo "Hello, world!";
Hello, world!

You can use the up and down arrows in interactive mode to recall previous code you ran.
7 years ago
Just a few more notes to add...

1) Hitting return does literally mean "execute this command".  Semicolon to note end of line is still required.  Meaning, doing the following will produce a parse error:

php > print "test"
php > print "asdf";

Whereas doing the following is just fine:

php > print "test"
php > ."asdf";

2) Fatal errors may eject you from the shell:

name@local:~$ php -a
php > asdf();

Fatal Error: call to undefined function...

3) User defined functions are not saved in history from shell session to shell session.

4) Should be obvious, but to quit the shell, just type "quit" at the php prompt.

5) In a sense, the shell interaction can be thought of as linearly following a regular php file, except it's live and dynamic.  If you define a function that you've already defined earlier in your current shell, you will receive a fatal "function already defined" error only upon entering that closing bracket.  And, although "including" a toolset of custom functions or a couple of script addon php files is rather handy, should you edit those files and wish to "reinclude" it again, you'll cause a fatal "function x already defined" error.
7 years ago
It seems the interactive shell cannot be made to work in WIN environments at the moment. 

Using "php://stdin", it shouldn't be too difficult to roll your own.  You can partially mimic the shell by calling this simple script (Note: Window's cmd already has an input history calling feature using the up/down keys, and that functionality will still be available during execution here):


= fopen("php://stdin", "r");
$in = '';
$in != "quit") {
"php> ";
    eval (

Replace 'eval' with code to parse the input string, validate it using is_callable and other variable handling functions, catch fatal errors before they happen, allow line-by-line function defining, etc.  Though Readline is not available in Windows, for more tips and examples for workarounds, see
lee8oi at gmail dot com
5 years ago
I use git-bash in windows to connect to my servers via SSH. When I use the interactive mode via 'php -a' command I have to hit ctrl+d twice to execute the entered code. Example:
(<ctrl+d> denotes hitting ctrl & D)

-bash$ php -a
Interactive mode enabled
echo 'hello world';
?><br />
hello world<br />

Note: this still displays the <br /> tag but without the tag your output would likely be attached to your bash prompt like this:

hello world-bash$
elijah at elijahlynn dot net
2 years ago
Bug #55496 Interactive mode doesn't force a newline before the prompt =>

Fixed on July 24th, 2014 @;a=commit;h=71d3a69425449972f4efdf7228c6f7e49e090755

Until then, this will work:

php -dcli.prompt="\nphp> " -a
alexandrebr at gmail dot com
6 years ago
For those who (just like me) can't get it working, try to press CTRL+D after inserting some commands.

echo "Hello World!\r\n";
Hit CTRL+D here)
Hello World!

This is NOT interactive mode, but may help you.

To have the "-i" available, you'll need the following arguments while compiling PHP:
--with-readline e --with-libedit
18 days ago
For use interactive mode enabled on GNU/Linux on distros Debian/Ubuntu/LinuxMint you must install "php*-cli" and "php*-readline" packages from official repository.
>$sudo aptitude install php5-cli php5-readline

After that you can use interactive mode.
~ $ php -a
Interactive mode enabled

php >echo "hola mundo!\n";
hola mundo!
php >

I hope somebody help it!
Shane Harter
4 years ago
If you've ever wanted to build your own interactive shell, I released a project recently that makes it insanely easy to build awesome shell apps in PHP. It blends features from Zend2 and Symonfy2 with things like regex routing, state management, etc. Check it out here:
6 years ago
When building php on FreeBSD from ports one can add --with-readline option by manually editing the var CONFIGURE_ARGS in Makefile inside the php port directory and proceeding with build as usual.
To Top