engineering

Debugging PHP with Xdebug and MacGDBp

Step through your PHP code with Xdebug.

By: Chris Saylor | October 20, 2012 • 2 minute read

Note: This is an article restored from archive. There zero reason not to use an IDE that has good PHP xdebug support at this point. The selection of PHP friendly IDEs at this time was poor.

Sometimes tracking down the source of a particularly frustrating bug can be tedious. Simply running var_dump() here and there is problematic and not very elegant. Sometimes it’s nice to step through the code line by line and examine the values of variables at the point of execution.

There are several ways to accomplish the above, but on my setup (Mac + VirtualBox), I prefer MacGDBp.

To get it running, first you need to make sure Xdebug is installed. For me, I run Ubuntu in a VM, so apt-get install php5-xdebug is enough to get me running. Next, you need to configure Xdebug to broadcast the debugging information and flow control over a port that MacGDBp can listen to:

[xdebug]
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.remote_connect_back=1

Note: Even though this is an old article restored from archive, this configuration still holds true for most modern setups.

Now your system has everything you need to listen to the dbgp protocol. After installing MacGDBp, running your php code will automatically stop progress and show up in the debug display. You can configure this to not stop on first launch and instead set breakpoints. Since Xdebug is an extension, you can insert xdebug_break(); at any point in your code to stop execution and step through using this interface.

With just a small amount of effort, you can have an environment for properly debugging your code easily and effectively.

Related Content