Random writings on software development, running, business and anything else

Introduction to PHP with Xdebug

This article is an introduction to some of the ways Xdebug can help you with your PHP code. Xdebug is a stable and mature PHP extension that can be invaluable in debugging and profiling your PHP code.
The examples here are kept deliberately simple. It is assumed you already have PHP and Xdebug installed on your system. Examples and output presented are from Ubuntu 8.10 but differences with other systems will be minor.
The current version of Xdebug is 2.0.3 and it works with PHP 4.3 to 5.3. Windows and UNIX type systems are supported. Xdebug uses the Xdebug license which is based on the PHP license.

Xdebug Configuration

On many operating systems, Xdebug configuration is in php.ini. On the Ubuntu setup with PHP 5 it is in /etc/php5/conf.d/xdebug.ini, but this will be varied to your particular needs.

Configuration settings control when and where trace information is captured, and to what level of detail. Collecting all possible output and you can quickly end up with a very large file. These configuration settings and others are explained in detail in the official documentation. Restart your web server when you modify your Xdebug settings, and make sure the trace_output_dir is writeable by the web server user.

Xdebug trace

Lets start with some simple code with an obvious bug and see what we get:

If you execute this code you can see a problem. The way many programmers would attack this problem is the add echo’s within the code and track down the problem. Obviously the bigger the problem, the harder this approach becomes. And who has not left debug code behind. Xdebug offers an alternative, so lets first look at some Xdebug trace output:

From this very simple example we can see useful code profiling information. The 1st column is a running execution time. Secondly is the amount of memory used. Third is the incremental amount of memory used and lastly the function call made and where from. Given the squareNumber function is called 5 times we get 1 line per call.
If we modify the Xdebug configuration slightly we can gather greater output for our debugging purposes.

xdebug.collect_params = 4
xdebug.collect_return = On

Restart the web server, rerun the example, and as can be seen below we now have details on function inputs and return values which can be helpful in script debugging. In this case our very obvious bug is there to be seen. The input is correct, but function output is always zero. Fix the code to $number * $number and we get much better results.

This simple example has only scratched the surface of the power of Xdebug and how it can help your code debugging. In a safe dev environment, play around with more complex code and Xdebug config options.

For very large code move the xdebug_start_trace() function call closer to a potential problem, and close down the trace with xdebug_stop_trace(). Never leave these calls in production code, as it slows down processing.