FirePHP is a useful tool that allows developers to output PHP debug messages directly into Firebug, so they’re displayed just like JavaScript console messages.
This guide assumes you’re using Firefox with the Firebug extension already installed.
Setting up FirePHP
With Firefox open, click Tools > Add-ons then search for “FirePHP” within the Get Add-ons tab and install the extension as usual.
Once it’s installed, and Firefox has restarted, open the Firebug window using the icon at the bottom-right of Firefox. Click on the Console and Net panels to ensure they’re both enabled. Also click on the blue FirePHP icon (near the top-left of Firebug) to make sure that’s enabled.
Now just make sure Firebug’s console is visible and you’re ready to start using FirePHP!
Getting your scripts ready
The FirePHP developers have written a library to make using their tool as easy as possible. Before you go any further download the FirePHPCore library from the this page and copy the included ‘FirePHPCore’ folder to somewhere that’s accessible by your website. Once you’ve done that you’re ready to include it in your PHP scripts.
The main thing to note about FirePHP is that it sends messages to Firebug using HTTP headers, so you cannot send anything to the browser (i.e. HTML output) until you’ve finished debugging with FirePHP. If you’ve got output_buffering enabled on your server this won’t be an issue but otherwise you will need to manually initiate a buffer from within your script using the ob_start() function.
require_once('FirePHPCore/fb.php'); ob_start();
Note: this guide assumes you are using PHP5. If you are running PHP4 please use the relevant files in FirePHPCore and refer the notes on FirePHP’s website if you run into problems.
Simple debugging
The simplest way to get debug messages into Firebug is by using the log method, which accepts a variable and a label as it’s respective parameters. FirePHP will automatically output it in a suitable way regardless of it’s type, even if it’s an array or an instance of an object.
FB::log($myvar, 'My variable'); // the label is optional
There are actually a few ways to call FirePHP’s methods but I find this approach the most straightforward. Don’t worry if you’re not familiar with the ‘FB::’ operator, it’s essentially just a convenient way of access the FirePHP object on a global scope. If you’d prefer to use more traditional object notation, or procedural code, scroll down to my final notes.
Specially formatted output
You’ll probably find that the log() method accounts for 90% of your FirePHP usage but sometimes it’s helpful to format a message depending on it’s context, like if you want to highlight a genuine error. The following output types are available.
FB::log('Typical debug message'); FB::info('Information message'); FB::warn('Script warning'); FB::error('Critical error');
FirePHP can also output tables which, amongst other things, can come in useful for debugging database work. Here’s an example of a simple table sent to Firebug but it could just as easily have been created from some database results.
$table = array(); $table[] = array('First name', 'Surname'); $table[] = array('James', 'Smith'); $table[] = array('Robert', 'Johnson'); $table[] = array('Mary', 'Williams'); $table[] = array('Linda', 'Jones'); FB::table('Names', $table);
It’s all very simple isn’t it!
Final notes
Debug information should never be visible on a live website so FirePHP makes to very simple to turn it’s output on and off. It’s important that you use this functionality as soon as you’ve finished debugging.
FB::setEnabled(false);
If you’re not comfortable using the ‘FB::’ operator there are other ways of accessing FirePHP’s functionality. Very briefly these are as follows:
Basic object oriented calls
require_once('FirePHPCore/FirePHP.class.php'); $firephp = new FirePHP(); $firephp->log($myvar, 'My variable');
Procedural calls
require_once('FirePHPCore/fb.php'); fb($myvar, 'My variable'); fb($table, 'Query results', FirePHP::TABLE);
That’s all there is to using FirePHP. It’s so easy to get up and running, and even easier to use within your code so I hope you’ll give it a go. For me it’s made development so much easier because I no longer rely on inline debug messages that mess up page layouts.




