Recipe 20.1. Finding and Fixing Parse Errors

20.1.1. Problem

Your PHP script fails to run due fatal parse errors, and you want to find the problem quickly and continue coding.

20.1.2. Solution

Check the line that the PHP interpreter reports as having a problem. If that line is OK, work your way backward in the program until you find the problematic line.

Or use a PHP-aware development environment that will alert you to syntax errors as you code, and that can also help track down parse errors when they occur.

20.1.3. Discussion

Like most programming languages, the PHP interpreter is very picky about the way scripts are written. When things aren't written exactly as they they should be, the PHP interpreter will halt parsing and let you know that things aren't right. This is called a parse error.

Take this flawed program:

<?php if isset($user_firstname) {   print "Howdy, $user_firstname!"; } else {   print "Howdy!"; } ?>

Save that to a file called howdy.php and run it, and PHP will display this error message:

Parse error: syntax error, unexpected T_ISSET, expecting '(' in /var/www/howdy.php on line 2

Based on this message, we know that there's a problem on line 2'specifically, a syntax error; something about an unexpected T_ISSET.

When PHP parses scripts to convert them into a format that the computer can understand, it breaks each line down into chunks called tokens. There are dozens of tokens that PHP recognizes, and it knows the rules about what tokens are allowed to appear in what order in a line of PHP code. In the parse error above, the bit about an unexpected T_ISSET means that a T_ISSET token was encountered by the PHP interpreter where it's not supposed to be.

Reading a little further through the parse error, it's suddenly clear that PHP interpreter was expecting a '(' where it found the T_ISSET token. Looking back at line 2 of the program, sure enough, the open parenthesis is missing after the if and before the isset( ) function.

Some PHP-aware editing tools can alert you to these problems before you get to the stage of running the code and getting the parse error in the first place. Figure 20-1 shows our buggy program in Zend Studio 5, complete with advance warning of the parse error in our future.

Zend Studio 5 sees the parse error before it happens

It's not always as easy as going directly to the line that the parse error tells you to go to. Sometimes an error several lines prior to the one reported causes a problem that may not seem like a problem when it is encountered, but is a problem within the context of what is on the line that the parse error is referring to.

If you have difficulty finding the source of the error and don't have access to a debugging tool to help you root out the cause of the error, remember that when all else fails, commenting is your friend. Start by commenting out blocks of code before the line referred to in the parse error, and then rerunning the offending script. Through the process of elimination, you will eventually find the line causing the problem.

20.1.4. See Also

The PHP parser token cheatsheet at

PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445 © 2008-2017.
If you may any questions please contact us: