Correctly understand the meaning of PHP program error message _php Tutorial

Source: Internet
Author: User
Tags parse error php compiler php error
When we write a program, it is always inevitable to err on the wrong way, no matter how cautious. These errors often confuse the PHP compiler. If developers are unable to understand the meaning of compiler error messages, they 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: only when the error occurs does PHP recognize it (this is described in detail later in this article). It is for this reason that the compiler indicates that the line of error, on the surface, may be syntactically correct, or it may be a line that does not exist at all!

A better understanding of the error message can greatly save the time it takes to identify and correct the wrong content. Therefore, in this article, I will try to clarify a number of different types of PHP error messages, and how to correctly understand the meaning of the various error messages during the development process.

The content in this article is not related to the version of PHP that you are applying, because the various errors described in this article are not limited to specific errors for a particular 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 single line, you must first clarify the compiler's mechanism for parsing PHP code. I'm not going to discuss this in detail in this article, but we'll talk about some simple concepts that are more prone to errors.

Variable declaration

If you declare a variable in a statement, it is as follows:

$variable = ' value ';

The compiler first evaluates the value of the right half of the statement (that is, everything to the right of the equals 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 an incorrect syntax, a parsing error occurs.

Parsing errors

Parse Error: Parsing errors, unexpected t_while in C:\Program Files\apache group\apache\htdocs\script.php on line 19

Each time a previous error is identified, the parsing error occurs one after the other. Because PHP stops executing the script after the first parsing error, debugging and correcting this set of errors can be especially annoying.

Also, parsing errors have very little information and do not report the line number where the error is. The reason is that when an error occurs, the compiler determines that several lines of syntax appear to be valid until an invalid syntax is encountered, and the most likely scenario is that the expression uses a predefined word, such as;

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

Predefined terms include while, function, and so on, if PHP uses uses to evaluate your code. You can't use these predefined terms to name variables, and if you have to do this, PHP will report more errors, which you can't stand.

The following sample may help you with this issue. Please consult the PHP code shown below:

$b = "somevalue" if ($b = = "Somevalue") {print "Hello world!";}?>

The error is on the "$b =" line (a semicolon is missing at the end of the statement), so the error should be "Parse Error: 3rd line missing semicolon" right? And should not be judged by the parser:

Parse error:parse error, unexpected t_if in C:\Program files\apachegroup\apache\htdocs\ereg2.php on line 4 string 6

In line 4th, the syntax of the IF () statement is correct. So, what is the compiler getting confused about? The clue is the "Unexpected t_if" section. "Unexpected T_???" appears Error, it means that the compiler discovers where the predefined word should not appear. T_if represents IF (), T_while represents while (), t_for represents for (), and so on.

Fortunately, some of the reasons for the error are simple:

The statement does not use a semicolon (;) end, such as the example above. Missing quotation marks in string.

Some of the other common mistakes

The most common mistake I've ever seen is when you don't end a function or loop with curly braces (}), which is probably the most common and annoying mistake. The specific code is as follows:
    • Total 2 Pages:
    • Previous page
    • 1
    • 2
    • Next page

http://www.bkjia.com/PHPjc/364039.html www.bkjia.com true http://www.bkjia.com/PHPjc/364039.html techarticle when we write a program, it is always inevitable to err on the wrong way, no matter how cautious. These errors often confuse the PHP compiler. If the developer is unable to understand the meaning of the compiler error message ...

  • 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.