How to debug PHP with Firebug

Dieser Beitrag ist auf mehrfachen Wunsch auf Englisch geschrieben und stellt einen bereits veröffentlichten Beitrag auf deutsch dar.

Each software developer knows the problem: writing software is just one part, testing and debugging is the other one. In php, there are some ways:

  • you can use a debugger tool (like xdebug or dbg)
  • or you can just print your variables to the output.

I love to develop my code with objects. I have to output these objects to check, if they have the expected values because there is no really good free debugger tool for current PHP versions. All of you know, that this can be done with this code:


If you have complex objects, the output is annoying. The extension xdebug could help because it prints var_dump code in a readable way with colors.

But there is still one problem: the debug output will destroy your layout and you cannot check your system without destroying the design. There must be another solution: here it is:

If you are developing JavaScript code, you surely know the Firefox extension Firebug (documentation). This extensions is a fantastic tool to test your code, your output and your background traffic. But it also allows you to print debug messages with types:

  • log
  • info
  • warning
  • error

This script….

<script type="text/javascript">


You can see, that each message type has it’s own color.

You can print simple messages, but Firebug can do more: it can output objects: This script…

<script type="text/javascript">
var obj = new Object();
obj.var1 = "test";
obj.var2 = 3;

… creates…
objekt output
with details:
Object details

Now, it would be fantastic, if such behaviour would be possible in PHP. And the good message is: it is possible. You only need the possibility to use JSON (which is part of PHP 5.2 and can be loaded in previous versions – PECL).

Now, I have written a little function, which will help you to debug your PHP code. You can download the debug function for firebug freely, of course.

The debug function helps you to print the variable name, simple variables, arrays and even objects. Here you can see an example:

class test {
public $var1;
public $var2;
public $var3;
$test = new test();

You will get the output:
php object in Firebug

You can view each detail of the object like a JavaScript object:
php object details in Firebug

There are a lot of situations to use this function: p.e. „set_exception_handler“:

function handleExceptionError($exception) {
debug("File/ Line",str_replace("\\","/",$exception->getFile()) . "::" . $exception->getLine(),DEBUG_TYPE_ERR);

In this way, you can test and debug your code without destroying your layout.

Have fun!

10 Kommentare zu “How to debug PHP with Firebug

  1. I just had the same idea, googled to see if someone already did it, and here I am…

    A couple ideas:

    1) You could also log all errors, not only exceptions, to the console. That would allow you to debug production sites, without the visitor even noticing (unless of course, they have Firebug open). This is a very simple function that I use to do so:

    // Errors learn Greek (become Exceptions)
    function errors_learn_greek($severity, $message, $filename, $lineno) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);

    2) Instead of using a bunch of preg_replace, wouldn’t it be possible to do this „hard work“ on the client side, with a script that decodes JSON objects? I haven’t checked into this much yet, but I will. This would also take some load off your server (and hand it to the client, oh well).

    Thanks for your efforts!

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *