Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Your Email:
Webmaster Blog

Testing PHP with the interactive shell

by Akash Mehta


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.




Related Posts
» SimpleTest: Unit Testing for PHP
» And the winner of the most important security tip competition is…
» Tips for Successful Widget Branding
» Testing email routines in web applications
» Gmail Labs: Opt-In Experimental Features, Beta Testing Evolved
 


This post has 2 Responses so far.
  1. Matthew Turland Says:
    May 12th, 2008 at 6:10 pm

    I’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.

  2. Akash Mehta Says:
    May 14th, 2008 at 3:52 am

    @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.

Leave a Reply