The PHP interpreter supports running it in interactive mode by using the
--interactive (short equivalent:
-a) command-line switch. Running an interactive PHP shell can be useful when you need to quickly try code snippets. But, for this mode to be fully functional, PHP has to be compiled with readline support. Unfortunately, on CentOS PHP has not been compiled this way.
You can check the PHP compile-time configuration switches, by invoking the following command:
php --info | grep -i 'configure command'
While searching for a solution about this, I ran across an interesting project, phpa. From the developer:
phpa is an interactive command line shell for PHP.
It is a replacement for the interactive mode of the PHP interpreter (php -a), hence the name.
This software is public domain.
Before using it, make sure php-readline is installed:
yum install php-readline
I downloaded phpa and set the executable bit as shown below:
wget -O - http://david.acz.org/phpa/phpa.txt > ~/bin/phpa chmod +x ~/bin/phpa
I also had to fix the bad shebang:
sed -i 's#/usr/local/bin/php#/usr/bin/php#' ~/bin/phpa
Finally, an interactive PHP shell! Now running code snippets from the PHP manual is fun:
[rocky@arena ~]$ phpa PHP 5.1.6 (cli) (Jan 13 2010 17:05:38) [Linux] >>> $arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42)); >>> >>> echo $arr["somearray"]; // 5 5 >>> echo $arr["somearray"]; // 9 9 >>> echo $arr["somearray"]["a"]; // 42 42 >>>
Enjoy your new PHP interactive interpreter!
The PHP Interactive Interpreter by George Notaras, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.