Debug PHP with Firebug and FirePHP

Posted: February 10, 2010 in Forums, News, PHP
Tags: , ,
Debug PHP with Firebug and FirePHP

Debug PHP with Firebug and FirePHP

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:

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:

“require_once(‘FirePHPCore/fb.php’);”

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:

“ob_start();”
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:

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:
1 $var = array(‘a’=>’pizza’, ‘b’=>’cookies’, ‘c’=>’celery’);
2 fb($var);
3 fb($var, “An array”);
4 fb($var, FirePHP::WARN);
5 fb($var, FirePHP::INFO);
6 fb($var, ‘An array with an Error type’, FirePHP::ERROR);

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

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:
1 function hello() {
2 fb(‘Hello World!’, FirePHP::TRACE);
3 }
4 function greet() {
5 hello();
6 }
7 greet();

Will produce an output as follows:

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.

Advertisements
Comments
  1. Amber Carr says:

    $author Thank you so much for a wonderful blog. It was such a great article. Have a great day!

  2. fear to fly says:

    Hi there! I simply would like to offer you a huge thumbs up for your great info
    you have right here on this post. I am coming back to your site for more soon.

  3. Elijah says:

    Who writes this stuff? It’s freaking fantastic! I could use a article author on my own web sites if the quality is this outstanding. Care to reveal the name and contact info of the article writer? If not, no biggie. I comprehend the need to keep your writers secret.

  4. Superb post however , I was wondering if you could write a litte more on
    this topic? I’d be very thankful if you could elaborate a little bit further. Thanks!

  5. Francine says:

    Superb info, but I have got a quick question… who created your web template?
    Did you build it yourself or is this a high grade design?
    If it is totally free, I’d love to get it, it’s precisely the
    type of theme I require for my upcoming website.

  6. I blog quite often and I seriously thank you for your content.

    Your article has truly peaked my interest. I will bookmark your blog and keep
    checking for new information about once per week.
    I subscribed to your Feed too.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s