When it comes to trying out a snippet of PHP, the process can often be time consuming and tedious. Loading up your editor, saving a new file in your http docs folder, firing up a webserver, running some tests and then clearing the files off – it’s generally more trouble than its worth. Thankfully, PHP provides the interactive shell, allowing you to test out PHP interactively with immediate feedback. Here’s how to take advantage of this mature feature of PHP.
Make sure the folder with your PHP interpreter (where php.exe or the compiled php binary is stored) is in your system’s PATH. Here are some instructions for linux and windows. You can test that PHP is in your path by firing up a command prompt / console window and running “php -v”. You should see your PHP version.
Provided you have a reasonably recent version of PHP (any version of PHP 5 is sufficient), you’re now all set to use PHP interactive mode! Open up a command prompt / console window and run “php -a”. You will see a message saying “Interactive mode enabled.” Next, type out a PHP open tag –
There are a few quirks to remember when working with interactive mode.
First, avoid jumping in and out of code blocks. When you close a PHP block, type out some text and open another PHP block, PHP will output everything between the last close and open tag.
Your statements can span multiple lines – but remember to put in the closing semicolon. This might be obvious in an IDE, but it’s a common mistake in a simple terminal.
If something isn’t working quite right, debug your variables with echo calls – that’s what the interactive shell is for.
Watch your current directory. You may find the __FILE__ magic constant useful. If you want to get the current directory, it’s the ‘dirname’ element of pathinfo(__FILE__). Generally, this will be the path under which you called php -a, but it can be slightly erratic. If you want to include other PHP files from the interactive shell (which can definitely be done, and is encouraged to test and play around with libraries), you may be safest using absolute paths.
Also, remember that you do not need to type in your PHP code to test it using the PHP binary. While this is outside the area of the interactive shell, calling “php filename.php” will execute filename.php as a standard PHP script, which may prove useful for cron jobs / scheduled tasks or just plain testing.





4 Responses to “Testing PHP with the interactive shell”
May 12, 2008
Matthew TurlandI’m not sure if it’s specific to a particular branch or version range, but I remember a point in time where I had to include a -q flag to run scripts via command line. Also, if you want to run a small number of commands, the -r flag is also useful; it allows you to pass PHP code to be executed in as a shell string value.
May 14, 2008
Akash Mehta@Matthew: The -q flag is actually to suppress HTTP header output. Before the `php` binary became the CLI one and `php-cgi` served web requests, running a script from command line would output all the usual headers. I think it’s deprecated from PHP 5; running a script through `php filename.php` produces no headers while the new `php-cgi` binary does.
May 8, 2009
spyderman4g63This may sound stupid, by how to do exit once you are in interactive mode?
March 2, 2010
David Lefkonspyderman4g63 … clicking ctrl + D will exit interactive mode