Debugging PHP With Firebug

Blog Projects
javascript php
HEADS UP! This article was written in 2007. It exists mostly for historical reasons. You might want to check modern solutions such as Clockwork extension for Chrome.

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),
 $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.

When asking a question include an URL to example page where the problem occurs. Even better is to make a Fiddle which demonstrates the problem. If you have longer code examples please use