Using Xdebug to debug PHP applications

February 25, 2011
blog author

Appno Blogger

Appnovation Coop

Xdebug is an incredibly powerful tool for any PHP developer. I'm barely going to scratch the surface of the functionality it provides but hopefully it encourages you to go ahead and play around with it yourself. Xdebug is reasonably simple to install on any platform. Precompiled Windows binaries or the source can be downloaded from the Xdebug site here. This is an excellent article on getting up and running for MAMP on OS X and most Linux distributions should have an Xdebug package.

Once you've got Xdebug installed the first thing you might notice is that it will take over PHP's default error output with a stack trace. If you're not familiar with the science behind a stack trace, as always, Wikipedia is incredibly helpful, but it can be simply thought of as a series of function calls which lead to the error. You can also manually display a stack trace by using the xdebug_print_function_stack(). This stack trace is incredibly useful in understanding the flow of an application and potentially tracking down the source of errors.

In addition to stack traces, it's possible to capture the entire series of function calls with a function trace. You can then analyze this function trace to determine which functions are called when. For example, given two modules that implement hook_nodeapi, which is triggered first? It is also possible to analyze the performance of an application using a PHP script available here. In addition to this, it's possible to have Xdebug work with an IDE such as Komodo, giving you interactive PHP application debugging.

As I said, this is barely the surface of what Xdebug is possible. If you're already using it, maybe you can share with us your favourite tips in the comments! Questions are also welcome.