PHP Error Handling Finding and Fixing Parse Errors - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Error Handling Finding and Fixing Parse Errors - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, June 30, 2019

PHP Error Handling Finding and Fixing Parse Errors

PHP Error Handling


Finding and Fixing Parse Errors


Problem

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

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.

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 down each line into chunks called tokens. PHP recognizes dozens of tokens, and it knows the rules about what tokens are allowed to appear in what order in a line of PHP code. In the preceding parse error, 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 the 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. Shows our buggy program in NetBeans IDE, complete with advance warning of the parse error in our future.
Figure NetBeans IDE 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.

No comments:

Post a Comment

Post Top Ad