When debugging problems in PHP, most of the time it's easiest to just add var_dump($foo); exit; in the middle of your script, and you can see the contents of $foo right in your browser. But if you have to do much more, this approach gets cumbersome pretty quickly. I've recently been using step debugging for harder to track down problems. It allows me to examine the state of things all the way through execution of a request, line by line, or skipping ahead to break points. This process also gives you more insight into everything else happening in a request, which can be useful when you're using frameworks or other 3rd party code in your application.

With PHP, the common extension for this is Xdebug. (See the remote debugging documentation here). Since my main text editor is VIM, I had to find a suitable plugin to talk to Xdebug. It turns out there are a few good blog posts already on this topic, so I won't duplicate the information here. This post does a great job of covering installation and usage of the Xdebug plugin for VIM by Seng Woo Shin, though it links to an older version of it. Here's a link to a newer one that Xdebug also links to. The Xdebug site links to another tutorial that's worth a read too.

Beyond the links above, I've discovered a couple of things that are worth sharing. First, there's a new FireFox extension, easy Xdebug, that is a real lifesaver. You just click the green bug on the bottom right, and it will try to start an Xdebug session with any request (rather than having to add XDEBUG_SESSION_START=1 to the query string parameters myself). This is particularly handy when dealing with POST or or AJAX requests. The Xdebug docs also link to similar tools for Chrome, Safari, and Opera.

Now, my debugging work flow looks like this:

Start the Xdebug session in FF by clicking the green bug

Set breakpoints in VIM

Press F5 in VIM to start listening for the request (it waits 5 seconds)

Perform that action I want to debug in FF

Press enter in VIM to enter the debug session

Debug

Press F6 to exit debugger, and fix the problem

The one gotcha that I keep running into is how to set breakpoints correctly. When you use this VIM plugin, you must put a breakpoint on executable code. If you put it on a blank line, or even on a line with just an opening try statement, your breakpoint will not work. Instead, you'll end up with an "error 5". This topic is briefly covered here and here.

Now, when I find myself using more than one or two var_dump() statements to debug something, I usually walk through the work flow above, and have things sorted out much more quickly than I used to.

UPDATE: There is a new VIM plugin that's much better for step debugging. Check out the announcement here: http://joncairns.com/2012/08/vdebug-a-dbgp-debugger-client-for-vim-supporting-php-python-perl-and-ruby/