Debugging PHP With Firebug
February 8th, 2007
Logging PHP and JavaScript errors to same window makes debugging easier. It also might be the cure to cancer and bring world peace. In general…
In a recent PHP + JavaScript project I realized how annoying it is to have debug information in more than one place. For PHP I use PEAR::Log package. Log is written to file. For JavaScript I use Firebug. Log is written to Firebug console. When something goes wrong you must switch between browser and terminal windows.
There must be a better way. I like both PEAR::Log and Firebug. Obvious thing was to combine them. Result is Firebug handler for PEAR::Log. It can currently be found only in CVS. Hopefully be handler will be included in next release of PEAR:Log package.
Example PHP usage below.
$log = &Log::singleton('firebug', '', 'PHP',
array('buffering' => true),
PEAR_LOG_DEBUG);
$log->log('Debug lorem ipsum.', PEAR_LOG_DEBUG);
$log->log('Info wisi enim ad minim veniam', PEAR_LOG_INFO);
$log->log('Warning est usus legentis in', PEAR_LOG_WARNING);
$log->log('Error est notare quam', PEAR_LOG_ERR);
If you have Firebug installed, enable it and check Log Firebug test page. You can also redirect all PHP errors to Firebug console.
UPDATE 20070208: You might want to check FirePHP which provides different approach. FirePHP is Firefox extension built on top of Firebug. It modifies request headers to include Accept: text/firephp. Debug data is sent back using text/firephp section of multipart/mixed HTTP response. Using FirePHP is more complicated but at the same time it can provide you with more flexibility.
13 Responses to “Debugging PHP With Firebug”
Sorry, comments are closed for this article.

February 9th, 2007 at 07:03 AM
I followed you here from pear-dev, and I must say that this is pretty hot. :)
February 12th, 2007 at 08:45 PM
Thanks! Idea came from need. I am glad others have found it usefull too.
February 13th, 2007 at 06:55 PM
The following article provides a solution that allows you to log from PHP to the Firebug Console without the requirement for PEAR::Log. (It also includes JavaScript code in the response though which is what FirePHP eliminates):
http://ajax.phpmagazine.net/2007/02/how_to_use_firebug_to_debug_ph.html
February 14th, 2007 at 03:28 PM
I saw a link to Buggy in jquery-discuss. Looks interesting. For me requiring PEAR::Log is not a burden though. Most of my code already uses it for logging.
Normally I use composite handler which consists of file and mail handlers. All log data goes to file. Anything critical such as failing database connection gets sent to mail handler. If I want to log also to Firebug console, all it takes is to add firebug handler into composite handler. No need to do other code changes.
In the end it is matter of taste. If already using PEAR::Log it makes sense to choose PEAR::Log and firebug handler over Buggy.
That said, PEAR::Log is just abstracted logging system with unified api to different log storages. FirePHP in other hand is better debugging tool. I am looking forward to use FirePHP for next larger project in the near future. It is a great companion to Firebug!
March 2nd, 2007 at 04:58 PM
Hi,
First of all, thanks for jquery-editable plugin (came here for it)
Some code suggestion:
you don’t need to use global $log; in your errorHandler, you can just use $log = Log::singleton(‘composite’);
March 7th, 2007 at 02:07 PM
Thanks. Changed it in example. Using globals was horrible style mistake…
You seem to be located in Estonia too?
March 8th, 2007 at 01:36 PM
Yes, in Tallinn, actually :-)
March 24th, 2008 at 10:44 PM
In your http://www.appelsiini.net/~tuupola/test/php_error_to_firebug.php page, you have a minor typo on your default: priority case statement. You have ”$priotity” instead of ”$priority”.
Super cool concept! My co-workers were bedazzled.
March 27th, 2008 at 10:17 AM
Fixed. Thanks for the heads up. Actually did not ever remember I still have some stuff under the /~tuupola/ url…
April 10th, 2008 at 06:37 AM
There have been lots of changes to FirePHP including the addition of being able to easily log to the Firebug console without affecting the content on your page.
Take another look at the homepage http://www.firephp.org/.
Thanks for the interest and support!
Christoph
April 10th, 2008 at 08:31 PM
Thanks for the heads up. It has been a while since I last looked at FirePHP. New features look really promising.
Keep up the good work!
January 2nd, 2009 at 02:15 AM
For those who are on mac, what about a real PHP debugger: macGDBP is out! :)
http://www.bluestatic.org/software/macgdbp/
January 11th, 2009 at 08:07 PM
tow: Looks interesting. Thanks for notifying :)