Debugging PHP, with breakpoints!

One thing that I have always disliked about PHP development was the seemingly difficult debugging process that came along with it.  The fact that you would save a file, and then have to open it in the browser to see what’s going on – sometimes to random results.  There was none of the usual ability to step through the code and create checkpoints – something that Java, .Net, and even JavaScript (since the web inspector came about) have had for a while.

Well it turns out that I was wrong. The ability to do this stuff exists, I was just unaware of it, and because of my willingness to accept the status quo, I never bothered to look for a way.

The missing piece of the puzzle is actually something that I have been using for a while. Xdebug. I have been using it to generate grind files, which allow you to profile your script using tools like WebGrind.

Add the following in your php.ini.

xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000

Then, in NetBeans, go to your options window and select the PHP tab.  Make sure that the Debugger Port corresponds to the one that you have set in your ini file.

Finally, restart your web server and have fun using breakpoints.

There is also a really useful guide from NetBeans on how to debug PHP using the inbuilt tools available to you.

Leave a Reply