Manual Debugging

As was mentioned earlier, once you get a few good years of development time under your belt, you should be able to get at least 75% of your debugging done on a purely visual basis. What of the other 25%, and the more difficult segments of code that you need to work through? Some of this can be alleviated by using a great code development environment like Zend Studio for Eclipse or Komodo. These advanced IDEs can help with syntax checking and some simple code logical problems and warnings.

The next level of debugging can be done (again, most of this will be done in the development environment) by echoing values out onto the screen. This will catch a lot of logic errors that may be dependent on the contents of variables. For example, how would you easily be able to see the value of the third iteration of a for...next loop? Consider the following code:

for ($j = 0; $j < 10; $j++) {
    $sample[] = $j * 12;
}

The easiest way is to interrupt the loop conditionally and echo out the value at the time; alternatively, you can wait until the loop is completed, as in this case since the loop is building an array. Here are some examples of how to determine that third iteration value (remember that array keys start with 0):

for ($j = 0; $j < 10; $j++) {
    $sample[] = $j * 12;

    if ($j == 3) {
      echo $sample[2];
    }
}

24

Here we are simply inserting a test (if statement) that will send a particular value to the browser when that condition is met. If you are having SQL syntax problems or failures, ...

Get Programming PHP, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.