By Louis Simoneau

Debug PHP with Firebug and FirePHP

By Louis Simoneau

If you’re anything like me, you’d sooner forgo water than Firebug when working on a web project. The little ’bug is a fantastically useful HTML/CSS/JavaScript/Ajax debugger. But did you know it can also be used to debug PHP? Yes, thanks to an additional Firefox extension called FirePHP.

By combining this extension, which sits on top of Firebug, with a server-side library, your PHP scripts will be able to send debugging information to the browser, handily encoded in the HTTP response headers. Once you’re set up, you can log warnings and errors in your PHP scripts to the Firebug console, just as if you were developing JavaScript.

To start, you first need to install the FirePHP extension from Mozilla’s Firefox Add-ons site. This requires that you already have Firebug installed. Once FirePHP is installed, when you next open your Firebug panel, you’ll now see an additional blue bug. Click on that bug and a menu will appear allowing you to enable or disable FirePHP:

FirePHP Menu

This, of course, won’t do anything yet. You also need to install the FirePHP server-side library, which is available here. This is a stand-alone version of the library that can either be downloaded manually or installed using PEAR. After that, you simply need to include the library in your code. There are also versions designed to integrate with various frameworks or content management systems, such as the WP-FirePHP plugin for WordPress or the JFirePHP plugin for Joomla. For the sake of this walk-through, I’ll focus on the stand-alone functionality.

Once you have the FirePHP library on your server, you need to include it in your script with a line like:


Because FirePHP sends its logging data via the HTTP headers, you’ll need to buffer your script’s output so that the response headers can include content generated further down the script. In PHP, this is accomplished by calling ob_start near the top of your script:


With these steps done, you can start using FirePHP. All you need to do is call the fb function with whatever you’d like to log, along with an optional label and an optional constant to define the message as a standard log, a warning, an error, or information. For example:

$var = array('a'=>'pizza', 'b'=>'cookies', 'c'=>'celery');
fb($var, "An array");
fb($var, FirePHP::WARN);
fb($var, FirePHP::INFO);
fb($var, 'An array with an Error type', FirePHP::ERROR);

This code will produce the following output in the Firebug console:

FirePHP Console Output

You can also use FirePHP to give you a trace of your application’s execution: by passing in the FirePHP::TRACE constant, you’ll get to see the line number, class name, and function name from within which fb was called. So this code:

function hello() {
  fb('Hello World!', FirePHP::TRACE);
function greet() {

Will produce an output as follows:

FirePHP Trace Output

This trace functionality can be fantastic for debugging more involved scripts, as it lets you know exactly from where your functions are being called.

Of course, you need to remember to remove your debugging statements before your code goes live!

There’s a lot more to FirePHP than what I’ve covered here. I’ve been showing you the simplified procedural API for FirePHP, but there’s a more advanced object-oriented API available with a number of additional features. You can learn all about it on the FirePHP site, so be sure to check it out.

The most important and interesting stories in tech. Straight to your inbox, daily. Get Versioning.
Login or Create Account to Comment
Login Create Account