When we write a program, no matter how careful, make mistakes are always inevitable. These errors typically confuse the PHP compiler. If the developer can't understand the meaning of the compiler error message, then these error messages are not only useless, but they will often be frustrated.
When compiling the PHP script, the PHP compiler will do everything they can report the first problem it encountered. This creates a problem: "This problem is described in detail later when the error occurs). It is because of this reason, the compiler points out the line of error, which seems to be correct from the surface, or maybe it does not exist at all!
Better understanding of error messages can greatly save time spending and correcting error content. Therefore, in this article, I will try to clarify how many different types of PHP error messages, and how to correctly understand the meaning of various error information during the development process.
The content described in this article is independent of the version of the PHP you applies, as the various errors described herein are not limited to a particular version of a particular error. In addition, we assume that you are a primary or intermediate programmer and have been engaged in programming for half a year or year.
Compiler work mode
To figure out why the compiler reports that there is an error on a row, we must first define the mechanism of the PHP code. I am not going to discuss this in this paper, but we will discuss some simple concepts that are more likely to cause errors.
Variable declaration
If a variable is declared in a statement, the specific method is as follows:
$ Variable = 'Value';
The compiler first determines the value of the rightmost half of the state (ie all the contents of the right). In some programming books, this is expressed as the RHS of the statement (right half). It is precisely that this part of the statement often triggers an error. If the syntax used is incorrect, an analysis error occurs.
Analytical error
PARSE ERROR: Analysis Error, Unexpected T_While In C: / Program Files / Apache Group / Apache / HTDOCS / Script.php Online 19
Each time a previous error is determined, the error is constantly appearing one by one. Because PHP stops executing scripts after the first parsing error, debug and correct this series of errors will often feel particularly bored.
Moreover, parsing errors have very little information, and there is hardly not reporting 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 be effective until an invalid grammar is encountered, the most likely case is that the predefined word is used in the expression, for example;
While = 10; // bad? While is a predefined word and cannot be assigned to a value
The predefined words include While, Function, etc. If PHP uses Uses to Evaluate Your Code. You cannot use these predefined words to name the variable, and if you have to do this, PHP will report more errors. This is where you can't stand it.
About this problem, the following example may help you. Please consult the PHP code shown below:
PHP $ b = "SomeValue" if ($ b == "somevalue") {print "Hello World!";}?>
The error is located in "$ b =" one line (the missing number at the end of the statement), so the error should be "parsing the error: line 3 lack of semicolon" right? And should not be determined according to the parser:
PARSE ERROR: PARSE ERROR, UNEXPECTED T_IF IN C: / Program Files / ApacheGroup / Apache / HTDOCS / EREG2.PHP On Line 4 The syntax of the IF () statement is correct. So, what is the compiler to be confused? The clue is the "UNEXPECTED T_IF" section. When the "Unexpected T _ ???" error occurs, the meaning it represents is that the compiler found that the predefined word should not appear. T_IF represents if (), t_while represents while (), t_for represents for (), etc.
Fortunately, some errors are also very simple:
The statement does not use the semicolon (;), such as the example above. Quantitative marks in the string.
Other common mistakes
The most common mistake I have ever seen is that this is probably the most common and annoying mistake when there is no buder (}) ending a function or a loop. The specific code is as follows:
Function uselessFunction () {for ($ I <0; $ I <10; $ I ) {}
The following errors are generated:
PARSE Error: Parse Error, Unexpected $ IN C: / Program Files / Apache Group / Apache / HTDOCS / EREG2.PHP on line 9
Since the function uselessFunction does not use braces (}) to end, the PHP compiler constantly looks out to indicate the bracket end until the end of the file. Because the compiler did not find a matching brace, there was an error at the end of the file.
If the hierarchy of the code is correctly reflected, the error message will become very obvious. If there is no hierarchy of the code, then, if you want to find out what you have forgotten, it will be almost impossible. So, remember, be sure to indicate the hierarchy of the code. This is easy to achieve this. For subsequent developers, grasp the code framework and modify it will be more easily.
MySQL error
Another extremely annoying error message is the most common mysql error, which often makes PHP novices quite a headache:
WARNING: Supplied Argument is not a valid mysql result resource in ...
One of the wrongdies that have been reported above may be:
While ($ row = mysql_fetch_array ($ result)) {
Parameter $ Result is not a valid resource. In English, it means that because the query fails, mysql_fetch_array will not be processed. The syntax of any query is invalid (you should test the query copy - paste to the MySQL console reference), or fail with the database (in this case you should check the username and password again).
Prevent errors
The first step, the smart code can take the following steps to eliminate the following errors:
· At the end of each statement, you don't have to consider adding a semicolon - this should become a habit.
· Always demonstrate the hierarchy of the code as much as possible, which allows you to see if you forget to add braces to the end of the IF call or function.
• Use an editor that highlights the syntax (such as HTML-KIT). With the aid of this type of editor, you can determine if I forgot to add quotes, whether or not the semicolon, etc.
in conclusion