How to correctly understand PHP's error message

Source: Internet
Author: User

When we write a program, no matter how cautious, mistakes are always unavoidable. These errors usually confuse the PHP compiler. If developers cannot understand the implications of compiler error messages, these are not only useless, but often frustrating.

When compiling a PHP script, the PHP compiler will do its best to report the first problem it encounters. This creates a problem: PHP can recognize the error only when it appears (a specific description of the problem is described later in this article). It is for this reason that the compiler points out that the line that went wrong may appear to be grammatically correct, or perhaps a line that does not exist at all!

A better understanding of the error message can greatly reduce the time it takes to determine and correct the error content. Therefore, in this article, I will try to clarify a variety of different types of PHP error information, as well as in the development process to correctly understand the meaning of various error messages.

The content described in this article has nothing to do with the version of PHP that you are applying, because the various errors depicted in this article are not limited to specific errors in an extraordinary version. In addition, we assume that you are a beginner or intermediate programmer and have been working on programming for six months or a year.

How the compiler works

To figure out why the compiler reports errors on a line, you first have to be clear about the compiler's mechanism for parsing the PHP code. I'm not going to be specific about this in this article, but we're going to discuss some simple concepts that make it easier to raise bugs.

Variable declaration

If you declare a variable in a statement, the way it looks is as follows:
$variable = ' value ';
The compiler first asks for the value of the right half of the statement (that is, everything on the right side of the equal sign). In some programming books, this is represented as the RHS (right half) of the statement. It is precisely this part of the statement that often throws an error. If you use a syntax that is not correct, a parse error occurs.

Parse error

Parse Error: Parsing errors, unexpected t_while in C:Program Filesapache groupapachehtdocsscript.php on line 19

Each time you determine the previous error, the parse error continues to appear one after another. Because PHP stops executing the script after the first parse error, debugging and correcting this series of errors can be especially annoying.

Also, parsing errors have very little information and do not report the line number where the error is located. The specific reason is that when an error occurs, the compiler determines that several lines of syntax should appear to be valid until it encounters an invalid syntax, the most likely scenario being the use of predefined words in an expression, for example;

while = 10; Bad? While is a predefined word and cannot be assigned to a value

Predefined words include while, function, and so on, if PHP uses uses to evaluate your code. You cannot use these predefined words to name variables, and if you do this, PHP will report more errors than you can bear.

The following example may help you with this issue. Please consult to read the following PHP code:

<?php
$b = "Somevalue"
if ($b = = "Somevalue") {
Print "Hello world!";
}
?>

The error is on line "$b =" (missing semicolon at the end of the statement), so the error should be "Parse Error: line 3rd missing semicolon" right? And should not be judged by the parser:
Parse error:parse error, unexpected t_if in C:Program Filesapache
groupapachehtdocsereg2.php on line 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.