Debugging PHP

Debugging is a fine art. The more experience with technology one gathers the faster he or she can debug. The ability to find the source of problems quickly is perhaps the experienced programmer’s greatest skill. It leads to great efficiency and better code. The difficulty is that for the most part debugging is an acquired skill. One has to have experienced many of the problems first hand to intuitively understand the nature of many problems that arise in technology or code.

The most important task is to determine the scope and source of the problem. Is it a network issue, a code issue, a user misunderstanding, etc? Some of these problems can’t easily be addressed by the programmer responsible for the code, but some can. And to that end, a programmer is well advised to sprinkle their development cycle with as much test and validation as is feasible. Write the tests before and during the code writing process. Test everything, fully understand your inputs and outputs. It is also advisable to develop techniques to quickly determine where the problems are. For PHP development this can be particularly difficult because often one does not have the benefit of a fully integrated developer environment. Web hackers tend to use more primitive tools than their desktop application building cousins. We use text editors, the browser, and Apache/PHP error logs to make miracles out of code.

In PHP I find it really useful to aggressively log everything while I am developing. One technique involves purposively generating error logs as a means of tracing the output of code, variables, and functions. It is doubly important to trace where that output comes from, in other words what is the line number of the code that this came from? Because, of this I find the following code pattern very useful with PHP development:


error_log( “Line ” . __LINE__ . ” : ” . $errorMessage, 0 );

The beauty of this line of code is that it logs to the error log exactly what you want. The first bit traces a line number and uses the PHP constant __LINE__ to get the line of code. Using this constant will always tell you where you are at. ANd it is dynamic so the code in your script can grow by thousands of lines and you will always know where you are at in the code. Secondly, I used the variable $errorMessage, but this can be anything you want. You could insert other variables or even trace the output of entire functions and complicated multi dimensional arrays. For example:


error_log( “Line ” . __LINE__ . ” : ” . print_r($myArray), 0 );

Will trace out the exact structure of your array. This array may be the product of multiple iterations and function calls so you can still see what it is. The main value is that you run the above line of code without affecting the output that goes to the browser. In fact you could place several instances of the above line throughout your code and always be able to look up the error logs and know exactly what happened in your code as it was running. This may not be as convenient as real time compiler checking of a full blown IDE, but when hacking away at PHP it can be pretty darn useful for tackling a tricky bug or complicated routine or function.