PHP is unforgiving of syntax errors. Misplaced a semicolon? White screen of death. Mismatched your single and double quotes? White screen of death. Considering a career in Ruby on Rails? White screen of death.

Okay, maybe not the last one, but you get the point. Unless you beg and plead with it to output errors, PHP will give you the computing equivalent of a blank stare. Even if you do get it to report the problem, because of the nature of syntax errors, the line it specifies will rarely be correct.

In my experience, there are two surefire ways to weed out pesky syntax errors in PHP.

Sequential Commenting

Proper programming is supposed to involve a step-by-step process of adding and testing small portions of code in sequence. If you do this properly, any problems can easily be attributed to the most recently added block of code and subsequently fixed.

Of course, any good developer knows that “proper” programming is an anomaly. So what do you do when you’ve written 517 lines of new code and encounter the white screen of death? Simple. Comment it all out, then uncomment and test small portions of it sequentially like you should have from the start. You get pretty much the same effect, albeit at slightly less efficiency.

Execution Counter

PHP will generally cease execution at the first point where it encounters a syntax error. By placing the following line of code at regular intervals throughout a broken program, you can use that fact to your advantage and quickly narrow down the problem.

For example, assume that you placed five counters like this throughout your code, and the final output is, “123.” Because the fourth counter failed to execute, you can safely assume that the problem occurred between it and the third counter.

Of course, you could just use a utility such as Dreamweaver or Eclipse that makes syntax errors much more obvious. Then again, what would be the point of programming in PHP without the challenge of guessing what went wrong? 😉

One thought on “Debugging Syntax Errors in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *