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.
Consola interactiva
Desde PHP 5.1.0, CLI SAPI 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.
Example #1 Ejecutando código desde la consola interactiva
$ 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 >
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.
Example #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 con cosas que se han definido durante la sesión de consola interactiva:
php > $fooEsteEsUnNombreDeVariableMuyLargo = 42; php > $foo[TAB]EsteEsUnNombreDeVariableMuyLargo
La consola interactiva almacena un historial, al que se puede acceder usando las teclas arriba y abajo. El historial se almacena en el fichero ~/.php_history.
Note:
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.
Note:
La auto-carga no está disponible al usar PHP en modo interactivo en CLI.
I rummaged through the internet and the manual so I could get to an interactive shell in a windows environment. I was trying to put the information into a note here, but it's too long.
It's a lot of words, but it's not hard to do. I've been keeping some notes on my website, but it's crap, and it's not always on. Maybe someone who knows what they're doing can get this where it needs to be? I think it's important to have all the instructions in the same place.
http://atropa.freeiz.com/?context=fetch_chapter&content_id=14
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):
<?php
$fp = fopen("php://stdin", "r");
$in = '';
while($in != "quit") {
echo "php> ";
$in=trim(fgets($fp));
eval ($in);
echo "\n";
}
?>
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 http://www.php.net/manual/en/ref.readline.php
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...
name@local:~$
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.
