Skip to content
 

Debugging with FirePHP

I just discovered FirePHP today (via a discussion at PHPBuilder.com), a set of PHP classes along with a Firefox add-on which allows you to debug your PHP scripts through the Firebug console (a separate Firefox add-on you’ll also need to install if you haven’t already done so).

Essentially, what it does is allow your PHP server-side scripts to output debug information to your browser via a set of HTTP headers which are then intercepted and displayed by the Firebug console window. This way, instead of doing a var_dump() or print_r() to view a variable or object in the middle of your HTML output (or routing it to a log file on the server), you can view that info separately in the Firebug window.

Here’s a quick example of the most basic usage:

<?php

ob_start();

require_once 'lib/FirePHPCore/FirePHP.class.php';

$fb = FirePHP::getInstance(true);

?>

<html><head><title>Test</title></head><body>

<h1>Hello, World!</h1>

<?php

$var = "This is a test";

$fb->log($var, 'Test');  // this will show up in FireBug console

?>

<p>The end</p>

</body></html>

When you launch this page in Firefox with FirePHP installed and enabled and the Firebug console displayed, you see:

FirePHP/Firebug sample output

In addition to simply outputting debug/log info, FirePHP can also be used as the default error-handler and/or exception-handler in a PHP script. I will be investigating that next, and will post the results and some sample code here soon….

Leave a Reply

You must be logged in to post a comment.