People who are cautious about writing a program will still make mistakes. The following describesPHPAnalysis of these small errors.
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 filesapache groupapachehtdocsscript. 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 filesapachegroupapachehtdocsereg2.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 filesapache groupapachehtdocsereg2.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:
- At the end of each statement, you do not have to consider adding a semicolon-this 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 highlights 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 points are 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.