Print this page

Debugging PHP by Firebug tools

Debugging PHP scripts is definitely not enough described in the Internet. That is why a great amount of people are content with print_r. The obvious drawback of this method is impossibility to debug AJAX, SOAP services, image generators, and generally scripts not giving HTML documents directly.

Javascript developers use Firebug for debugging. I have always envied them. It’s cool to have a console, net monitoring and a debugger; all this stuff is in your favorite browser.

So, I have found such Firebug extension as FirePHP. It allows to output information into Firebug console directly from PHP. This is done by a simple execution:

  1. require "FirePHP.class.php";
  2. $firephp = FirePHP::getInstance(true);
  3. $firephp -> fb("hello world! i'm warning you!",FirePHP::WARN);

Also you may send arbitrary data structures and exceptions in Firebug. In the latter case we obtain not only exception object itself, but also stack contents. There are a lot of opportunities, you should read the documentation.

The advantage of such debugging is that data is not transferred to the body of a page, but in headers. This means that, firstly, the page does not foul up with all sorts of var_dump, and secondly, you can easily debug AJAX calls.

In order to use FirePHP you need to connect one file to the project and enable output buffering. That's all.

Speaking about buffering, FirePHP actually wants no one to write anything to the output stream before it. It is reasonable, because it sends the headers. If probably you have already been using buffering to send your own headers, then it is not a problem. I mean, it is not necessary to use ob_start(), as mentioned in the manual.

I do not advise to shove the calls fb() directly into your code. Think about what will happen to them on the production server. It is more correct to inject FirePHP to the debug system, for example, there are already extensions for Zend Framework and Symfony.

Read 3638 times