Efficient PHP Debugging In Vim

I mainly develop in PHP, which naturally involves a web server. This makes setting up a streamlined development environment a bit trickier. Nonetheless, we have all the necessary tools to make debugging PHP as powerfull as in any compiled language.

I use vim as my primary editor, so I’m going to show you how you can get all the features that are normally associated with IDEs.

Inline Syntax Checking

The typical development loop looks like this:

  1. Write code in editor.
  2. Refresh browser.
  3. Get parser error.
  4. Fix syntax error in editor.
  5. Refresh browser.

We can skip steps 2 and 3 by auto-linting the source code without leaving the editor. All you need to do is install the Syntastic plugin for vim. Each time you save the file, it will be run through php -l and if there’s an error, your cursor is automatically moved to that line.

Breakpoints, Stacktraces And Watched Expressions

Most of the time, sprinkling var_dump() calls through your code works well enough. But there are cases when chasing down a bug through a complex call chain is just hopeless using that approach.

Fortunately, we have Xdebug, which allows us to step through code as it’s being executed. If you’ve ever set a breakpoint in Firebug or in Visual Studio, you know what I’m talking about.

There are various vim extensions that can talk to Xdebug, but the best one I’ve found is called DBGPavim. In the readme, you will find all you need to know about setting it up.

Note that you don’t have to use Apache for this. I use nginx and php-fpm and Xdebug works fine.

To see other vim goodies I use, check out my dotvim repo on Github.

comments powered by Disqus