• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Testing PHP with the interactive shell

By Akash Mehta | on May 9, 2008 | 4 Comments
PHP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

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.

Share this story:
  • tweet

Author Description

4 Responses to “Testing PHP with the interactive shell”

  1. May 12, 2008

    Matthew Turland Log in to Reply

    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. May 14, 2008

    Akash Mehta Log in to Reply

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

  3. May 8, 2009

    spyderman4g63 Log in to Reply

    This may sound stupid, by how to do exit once you are in interactive mode?

  4. March 2, 2010

    David Lefkon Log in to Reply

    spyderman4g63 … clicking ctrl + D will exit interactive mode

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 1,239Followers 494Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.