How to correctly understand the meaning of PHP program error message _php Tutorial

Source: Internet
Author: User
Tags parse error php compiler php error
Summary: When we write a program, it is always unavoidable to make mistakes, 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 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? Instead of judging by the parser:

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

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:

function Uselessfunction () {for ($i < 0; $i < $i + +) {}

The following error will be generated:

Parse Error:parse error, unexpected $ in C:\Program Files\apache group\apache\htdocs\ereg2.php on line 9

Because the function uselessfunction does not end with curly braces (}), the PHP compiler constantly looks for curly braces that represent the end until the end of the file is reached. Because the compiler does not find a matching brace, it reports an error at the end of the file.

If the hierarchy of the code is correctly reflected, the error message becomes apparent. If you don't have a hierarchy of code, it's almost impossible to figure out what you're forgetting. So, remember, be sure to mark the hierarchy of your code. The TAB key can be easily implemented. It is also easier for subsequent developers to grasp the code framework and modify it.

MySQL Error

Another extremely annoying error message is the most common MySQL error, which often makes PHP novice feel a headache:

Warning:supplied argument is not a valid MySQL result resource in ...

The following line of errors reported above may be:

while ($row = Mysql_fetch_array ($result)) {

The parameter $result is not a valid resource. In English it indicates that the mysql_fetch_array cannot be processed because the query failed. The syntax for either query is invalid (you should copy the query-paste to the MySQL console reference for testing), or the connection to the database fails (in which case you should check the user name and password again, etc.).

Prevent errors from occurring

In the first step, the smart code can take the following steps to eliminate the following error occurrences:

· At the end of each statement, you do not have to consider adding semicolons-this should be a habit.

· Always try to indicate the hierarchy of the code as much as possible, which allows you to see if you have forgotten to add braces at places such as the if call or the end of the function.

· Use an editor that can highlight the syntax, such as Html-kit. With the help of this type of editor, you can determine if you have forgotten to add quotes, if you are missing semicolons, and so on.

Conclusion

In this article we have a certain understanding of some of the errors that the PHP compiler might be able to report that may seem meaningless. We need to apply what we have learned to how to avoid errors and how to correct them when they occur. Debugging is one of the most important parts of a developer's work. Improving commissioning efficiency can greatly speed up the overall work, shorten the time required to complete a project, and significantly reduce the mental stress associated with code failure.

http://www.bkjia.com/PHPjc/814355.html www.bkjia.com true http://www.bkjia.com/PHPjc/814355.html techarticle Summary: When we write a program, it is always unavoidable to make mistakes, no matter how cautious. These errors often confuse the PHP compiler. If the developer is unable to understand 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.