Tuesday, January 27, 2009

Understanding PHP error messages

Most important thing you need to know about before savoring the delights of PHP:

Error messages.

They’re an unfortunate fact of life, but it helps a great deal if you understand
what they’re trying to tell you. The following illustration shows the structure of a typical error message

Parse error: Syntax error, unexpected T_ECHO in c:\htdocs\testproject\test.php on line 15

Here

Parse error : resembles severity of error

unexpected : resembles what went wrong

File path : resembles where it went wrong

The first thing to realize about PHP error messages is that they report the line where PHP discovered a problem. Most newcomers—quite naturally—assume that’s where they’ve got to look for their mistake. Wrong . . . (Error messages
always prefix PHP elements with T_, which stands for token. Just ignore it.)

Instead of worrying what might be wrong with the echo command (probably nothing),
start working backward, looking for anything that might be missing. Usually, it’s a semicolon or closing quote on a previous line.

There are four main categories of error, presented here in descending order of importance:

Fatal error: Any XHTML output preceding the error will be displayed, but once the error is encountered—as the name suggests—everything else is killed stone dead. A fatal error is normally caused by referring to a nonexistent file or function.

Parse error: This means there’s a mistake in your code, such as mismatched quotes, or a missing semicolon or closing brace. Like a fatal error, it stops the script in its tracks and doesn’t even allow any XHTML output to be displayed.

Warning: This alerts you to a serious problem, such as a missing include file. However, the error is not serious enough to prevent the rest of the script from being executed.

Notice: This advises you about relatively minor issues, such as the use of deprecated code or a non declared variable. Although this type of error won’t stop your page from displaying (and you can turn off the display of notices), you should always try to eliminate them. Any error is a threat to your output.

There is a fifth type of error: strict, which was introduced in PHP 5.0.0, mainly for the benefit of advanced developers. Strict error messages warn you about the use of deprecated code or techniques that aren’t recommended.

0 comments:

Post a Comment