2008.07.01

I finally got around to installing Xdebug on my development environment and have decided it is the best thing since sliced bread. Installation was a breeze and the information it provides when something has gone wrong is incredibly helpful during debugging. What I didn't know, and hope to help others by documenting it here, was the amount of configuration options Xdebug has. The base install has some irritating limitations that are easily addressed with a few simple lines in the php.ini file.

Overview

Let's take a step back and describe exactly what we're dealing with here. Xdebug is a PHP extension meaning it is self-contained in a dll file and all you have to do is tell php to use it. The same way you turn on the PHP GD-image or cURL libraries is the same way you enable Xdebug.

Xdebug comes into play whenever PHP decides it needs to output an error, warning, notice, etc and it does so automatically. Instead of the plain old PHP error saying that you have a syntax error on line 84 of script test.php Xdebug will give you a stack trace including memory allocation, function parameters, execution time, and so much more.

Here is an example output:

Xdebug

All that information could take some time to manually dump out to the screen. Most of the time you probably won't need it during debugging but when you do it's a godsend.

var_dump() is my go-to function when debugging and Xdebug even makes that easier to read with some formatting and highlighting assistance.

Xdebug var_dump

Install and Configure

To install Xdebug just follow these easy steps:

  1. Download the latest version of Xdebug from their site http://www.xdebug.org/.
  2. Drop the .dll into your PHP extensions directory - Usually /php/ext/
  3. Add this line to your php.ini file (Obviously replace the filename if your version is different than mine):
    extension=php_xdebug-2.0.3-5.2.5.dll
  4. Configure Xdebug
    • Here is my list of optional configuration settings I have:
      xdebug.collect_params=4
      xdebug
      .var_display_max_depth=999
      xdebug
      .dump.POST=*
      xdebug.dump.GET=*
      xdebug.show_local_vars=1

    • collect_params = 4
      • Displays the variables passed as arguments to certain functions
      • Without this, only function calls are shown and no information about its arguments.
      • More information can be found on their Stack Trace Page.
    • var_display_max_depth=999
      • gives me a complete, un-abridged, variable dump when using var_dump
      • By default, Xdebug will only show a couple levels of nesting. I found this irritating as I wanted to see everything when I var_dumped something.
    • POST and GET =*
      • Dumps anything in either variable.
      • I could spend an extra couple of seconds printing out these variables manually but with this option, they are always infront of me during an error. Very helpful.
    • show_local_vars = 1
      • Displays any variables accessable in scope of the faulting function or method.
    • Add these to the bottom of your PHP.ini file.
    • http://xdebug.org/docs/

It's important to note that absolutely no configuration is required apart from requiring the Xdebug extension in php.ini. Things will function perfectly using their default configuration but after going through their documentation I found several things that I thought would be useful and wanted to explain how to use a few of them in this article.

If you have any problems, check out Xdebug's Installation Documentation or leave a comment here and I'll try my best to find an answer.

Also Checkout

The guys over at the Zend Developer Zone have written up a great series about installing, configuring, and using all the advanced features of Xdebug. You can find that here:
http://devzone.zend.com/article/2803-Introducing-xdebug

Get my RSS Feed!

Comments

Tarique Sani on (7.2.2008 6:58 am) says

A HowTo on installing Xdebug on XAMPP http://www.sanisoft.com/blog/2007/06/23/how-to-install-xdebug-php-extension-for-xampp-on-linux/

 

Ortzinator on (7.2.2008 3:27 pm) says

Totally agree. The var_dump formatting alone is worth it.

 

Joakim Nygård on (7.5.2008 6:48 am) says

Xdebug certainly makes for much more enjoyable debug sessions.

Xdebug provides profiling functionality for finding bottlenecks in PHP code as well. I've recently released a web frontend for just this. It's called webgrind and is available from http://webgrind.googlecode.com

 

Salimane Adjao Moustapha on (7.5.2008 9:03 pm) says

A great In detail on how to install xdebug on linux
http://theindexer.wordpress.com/2008/06/11/installing-xdebug-on-xampp-for-linux/
And How to profile with xdebug on linux
http://theindexer.wordpress.com/2008/06/30/profiling-with-xdebug/

 

BrentNewland on (8.7.2009 9:05 pm) says

WinCacheGrind is indispensable when used with XDebug. It lets you open up xdebug traces and determine exactly how long each process in a script takes, letting you super optimize it (among other functions I haven't used).

 
* Name
* Email (Will not be displayed)
Website