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

Source: Internet
Author: User
Tags parse error php compiler

Summary: no matter how careful we are when writing a program, mistakes are always inevitable. These errors often confuse the PHP compiler. If developers cannot understand the meaning of the compiler error messages, these error messages are not only useless, but often frustrating.

When writing a program, no matter how careful we are, making mistakes is always inevitable. These errors often confuse the PHP compiler. If developers cannot understand the meaning of the compiler error messages, these error messages 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 encountered. In this case, a problem occurs: PHP can recognize the error only when it occurs (this article will detail the problem later ). For this reason, the compiler points out the wrong line. On the surface, the syntax may be correct, or it may not exist at all!

A better understanding of the error information can greatly save the time required to identify and correct the error content. Therefore, in this article, I will try to clarify various types of PHP error messages, and how to correctly understand the meaning of various error messages during development.

The content described in this article has nothing to do with the PHP version you have applied, because the errors described in this article are not limited to specific errors of a specific version. In addition, we assume that you are a beginner or intermediate programmer and have been engaged in programming for six months or a year. How the compiler works

To understand why the compiler reports errors on a certain line, you must first clarify the mechanism by which the compiler parses PHP code. I am not going to discuss this in detail in this article, but we will discuss some simple concepts that are easier to cause errors.

Variable Declaration

To declare a variable in a statement, follow these steps:

$ Variable = 'value ';

The compiler first calculates the value of the right half of the statement (that is, all content on the right 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 leads to errors. If the syntax used is incorrect, A parsing error occurs.

Parsing error

Parse error: parsing error, unexpected T_WHILE in c: \ program files \ apache group \ apache \ htdocs \ script. php on line 19

Each time the previous error is identified, the parsing error occurs one by one. Because PHP stops executing the script after the first parsing error. debugging and correcting this series of errors often make people feel particularly bored.

In addition, parsing errors have very little information, and almost no error row is reported. The specific cause is that when an error occurs, the compiler determines that several lines of syntax should appear valid until an invalid syntax is encountered. The most likely case is that the expression uses a predefined word, for example;

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

Predefined terms include while and function. If PHP uses to evaluate your code. you cannot use these predefined words to name variables, and if you have to do so, PHP will report more errors, which you cannot bear.

The following example may be helpful to you. Please consult and read the PHP code shown below:

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

The error is on the "$ B =" Line (the semicolon is missing at the end of the statement), so the error should be "Resolution error: the semicolon is missing in row 3rd", right? It should not be determined based on the Parser:

Parse error: parse error, unexpected T_IF in c: \ program files \ apachegroup \ apache \ htdocs \ ereg2.php on line 4

In row 3, the if () Statement syntax is correct. So what makes the compiler confused? The clue is in the "unexpected T_IF" section. "Unexpected T _??? "When an error occurs, it indicates that the compiler finds that a pre-defined word should not appear. T_IF indicates if (), T_WHILE indicates while (), and T_FOR indicates.

Fortunately, the cause of some errors is also very simple:

The statement does not end with a semicolon (;), as shown in the preceding example. The quotation marks are missing from the string.

Other common errors

The most common error I have seen is the error that occurs when a function or loop is not ended with braces (}). This is probably the most common and annoying error. The Code is as follows:

Function UselessFunction () {for ($ I <0; $ I <10; $ I ++ ){}

The following errors will occur:

Parse error: parse error, unexpected $ in c: \ program files \ apache group \ apache \ htdocs \ ereg2.php on line 9

Because the UselessFunction function does not end with braces (}), the PHP compiler keeps searching for braces indicating the end until the end of the file. Because the compiler does not find a matching braces, an error is reported at the end of the file.

If the hierarchy of the Code is correctly reflected, the error information becomes very obvious. If the hierarchy of the Code is not identified, it is almost impossible to find out what is forgotten. Therefore, remember to mark the code hierarchy. The Tab key can be easily implemented. For subsequent developers, it is easier to grasp and modify the code framework.

MySQL Error

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

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

The error line reported above may be:

While ($ row = mysql_fetch_array ($ result )){

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

Prevent errors

Step 1: The Intelligent code generator can take the following steps to eliminate the following errors:

· Adding a semicolon at the end of each statement should be a habit.

· Always mark the code hierarchy as much as possible, so that you can check whether you forget to add braces at the position such as if call or function end.

· Use an editor that can highlight the syntax (such as HTML-Kit ). With the help of this type of editor, you can determine whether you forget to add quotation marks and whether a semicolon is missing.

Conclusion

In this article, we have a certain understanding of some seemingly meaningless errors that can be reported by the PHP compiler. We need to apply what we have learned to how to avoid errors and how to correct errors when errors occur. Debugging is one of the most important parts of a developer's work. Improving the debugging efficiency can greatly speed up the entire process, shorten the time required to complete a project, and significantly reduce the mental pressure caused by Code failure.

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.