When we write a program, no matter how cautious, mistakes are always unavoidable. These errors usually confuse the PHP compiler. If developers cannot understand the implications of compiler error messages, these are not only useless, but often frustrating.
When compiling a PHP script, the PHP compiler will do everything it can to report the first problem it encounters. This creates a problem: PHP can recognize the error only when it appears (the problem is described in detail later in this article). It is for this reason that the compiler points out that the line that went wrong may appear to be grammatically correct, or perhaps a line that does not exist at all!
A better understanding of the error message can greatly reduce the time it takes to determine and correct the error content. Therefore, in this article, I will try to clarify a variety of different types of PHP error information, as well as in the development process to correctly understand the meaning of various error messages.
What is described in this article has nothing to do with the version of PHP that you are applying, because the various errors described in this article are not limited to specific errors in 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 line, you first have to be clear about the compiler's mechanism for parsing the PHP code. I'm not going to go into this in detail in this article, but we're going to discuss some simple concepts that make it easier to raise bugs.
Variable Declaration
If you declare a variable in a statement, you can do so in the following ways:
$variable = ' value ';
The compiler first asks for the value of the right half of the statement (that is, everything on the right side 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 throws an error. If you use a syntax that is not correct, a parse error occurs.
Parse Error
Parse Error: Parsing errors, unexpected t_while in C:\Program Files\apache group\apache\htdocs\script.php on line 19
Each time you determine the previous error, the parse error continues to appear one after another. Because PHP stops executing the script after the first parse error, debugging and correcting this series of errors is often a particularly annoying one.
Also, parsing errors have very little information and do not report 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 appear to be valid until an invalid syntax is encountered, and the most likely scenario is the use of predefined words in an expression, for example;
while = 10; Bad? While is a predefined word and cannot be assigned to a value
Predefined words include while, function, and so on, if PHP uses uses to evaluate your code. You cannot use these predefined words to name variables, and if you do so, PHP will report more errors than you can bear.
The following example may help you with this issue. Please consult to read the following PHP code:
<?php
$b = "Somevalue"
if ($b = = "Somevalue") {
Print "Hello world!";
}
?>
The error is on line "$b =" (missing semicolon at the end of the statement), so the error should be "Parse Error: line 3rd missing semicolon" right? And should not be judged by the parser:
Parse error:parse error, unexpected t_if in C:\Program Files\apache
group\apache\htdocs\ereg2.php on line 4
On line 4th, the syntax of the IF () statement is correct. So what did the compiler get confused about? The clue is the "Unexpected t_if" section. "Unexpected T_???" appears Error, it means that the compiler finds that a predefined word should not appear. T_if represents the IF (), T_while represents a while (), and t_for represents for ().
Thankfully, some of the reasons for the mistakes are simple:
The statement does not use semicolons (;) end, such as the example above. Quotation marks are missing from the string.
Some of the other common mistakes
The most common mistake I've seen is that when you don't use braces (}) to end a function or a loop error, this is probably the most common and annoying error. The specific code is as follows:
function Uselessfunction () {
for ($i < 0; $i < $i + +) {
}
The following error will occur:
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 finds the curly braces that represent the end of the file until the end of the document is reached. Because the compiler did not find a matching brace, an error was reported at the end of the file.
If the hierarchy of the code is correctly reflected, the error message becomes very obvious. If you don't have a hierarchy of code, then it's almost impossible to find out what you've forgotten in the end. So, keep in mind that you must indicate the hierarchy of your code. This can be easily achieved by the TAB key. It will be 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 novice PHP headaches: warning:supplied argument is not a valid MySQL result resource ...
The wrong line 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 to the MySQL console reference for testing), or the connection to the database fails (in which case you should check the username and password again).
prevent errors from occurring
In the first step, the smart code 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 try to identify the hierarchy of the code as much as possible, which allows you to see if you forget to add curly braces at places such as if calls or functions.
· 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 have forgotten to add quotes, missing semicolons, and so on.
Conclusion
In this article, we have a certain understanding of some of the errors that the PHP compiler can quote that may not seem to make sense. We need to apply what we have learned to how to avoid errors and how to correct errors when they occur. Debugging is one of the most important parts of all work for a developer. Improving the efficiency of debugging can greatly speed up the progress of the whole work, shorten the time required to complete a project, and also significantly reduce the mental stress caused by code failure.