debugging php with firebug and firephp

firephp is a plugin for firebug, the web development plugin for firefox, that allows php scripts to talk to a firebug panel.

firephp installs alongside firebug and provides a simple php library to bridge the two. firephp provides a window of insight into your php applications, with a simple debugging interface that won’t interfere with your page content. if you already use firebug on php-powered applications, firephp is definitely worth a look. here’s a quick guide to getting started.

firephp is implemented as a native firefox extension and is available from mozilla addons. after a basic setup procedure, you can start printing everything from status messages to multi-dimensional arrays straight to your firebug console. firephp achieves this using extension http headers – in particular, x-firephp-data headers in the response.

the php library provides a function, fb(), which handles sending data across to the client side. from here, you simply open up your firebug console, and your data is displayed in a highly functional manner.

to get started with firephp, grab the extension from mozilla addons and the core library from the firephp project page (the “download” link). you’ll need to install firebug first, then the firephp firefox addon. next we get to the php library – either add the “firephpcore” folder to somewhere in your include_path, or just pop it into your script directory, and include it:

require('firephpcore/fb.php');

remember to change the relative path as required (no pun intended). then, simply call the fb()function anywhere in your code, passing a single argument – the info you want to send to your firephp console. here are some examples:

fb('the value of $var is '.$var);fb('just logging this event.',firephp::log);fb('error in the email module.',firephp::error);fb($_server, '$_server array', firephp::log); try {  throw new exception('exception');} catch(exception $e) {  fb($e);}

as you can see, we can output basic messages, use firebug’s standard styles to identify message importance (also, info and warn), output labelled assocative arrays, and even debug exceptions – firephp will display a neat little stack trace.

remember that this information is sent via headers, so it needs to be output before any of your html. if you’re using a framework with view templates, this is probably not an issue, but otherwise, you can get around the problem by using output buffering. just run this line somewhere near the beginning of your script execution:

ob_start();

this will initialise output buffering, so that any html you output is held up till the end of script execution (or till the next call to ob_flush() and derivatives). calling the fb() function will then work fine any time before the end of your script, as the headers will be flushed immediately. (alternatively you can use php_flag output_buffering on in your .htaccess / apache virtual host configuration).

firephp has all sorts of neat tips and tricks – just check out the quick start guide and protocol reference. if you use the zend framework, have a look at the zend library for firephp; similar libraries exist for symfony and prado as well.