Being a Good Debugger, PHP style

One of the things I have found that irritates me needlessly are people who are impatient debuggers. Finding and fixing bugs is certainly one of the least enjoyable activities you have to put up with when writing code. It takes a certain amount of persistance for going through the code to make sure you’re understanding what it does (not what it’s supposed to do) balanced against knowing when to ask for help or a second set of opinions. A quick search of the web didn’t turn up many non-technical tips about debugging scripts. Debugging Web Applications is certainly worth a read. Here are some other tips for being a good debugger in PHP:

Use Xdebug

Install and use the X Debug extension, which replaces php’s error handling with more verbose output including a trace of the function calls leading up to your error. This will avoid tracing the program flow from the beginning.

Verify all of your assumptions

Use phpinfo() to verify php settings, such as your include path and what extensions are loaded. Don’t assume something is setup or installed without confirming. For example, if you have an error log, make sure your script/php is set to log to that file and not some other file.

Document code behavior

Add comments for particularly hairy sections of code. For one, if you can describe it in a comment, you’ll have a better understanding. Also, if you have to come back to this code later you’ll save yourself some time in figuring out what that code was supposed to do.

Ask for help with details

When you need to ask for help, the more details you can provide the better. It’s rarely productive to tell someone that script X is failing and to leave it at that. Indicate what you’ve tried, what you have checked, and what you’ve verified. Your colleague is in a better position to tell you what else to try as well as point out what you might have missed.

Other good articles on the web:

This list isn’t comprehensive by any means, if you have any other tips to share please leave a comment.