Home > Article > Backend Development > Analysis of situations that cause PHP code errors_PHP Tutorial
No matter how careful you are, you will still make mistakes when writing programs. The following mainly introduces the analysis of these small errors in PHP.
These errors usually confuse the PHP compiler. Compiler error messages are not only useless but often frustrating if developers don't understand what they mean.
No matter how careful we are when writing programs, mistakes are inevitable. These errors usually confuse the PHP compiler. Compiler error messages are not only useless but often frustrating if developers don't understand what they mean.
When compiling a PHP script, the PHP compiler will do its best to report the first problem it encounters. This creates a problem: PHP can only identify the error when it occurs (this problem is described in detail later in this article). It is for this reason that the compiler points out that the line in error may appear to be syntactically correct on the surface, or it may be a line that does not exist at all!
Better understanding of error messages can save a lot of determinism and the time it takes to correct erroneous content. Therefore, in this article, I will try to clarify the many different types of PHP error messages and how to correctly understand the meaning of various error messages during the development process.
The content described in this article has nothing to do with the version of PHP you are using, 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 junior or intermediate programmer and have been engaged in programming for half a year or a year. How the compiler works
To understand why the compiler reports an error on a certain line, you must first understand the mechanism by which the compiler parses PHP code. I'm not going to go into detail about this in this article, but we will discuss some simple concepts that are more likely to lead to errors.
Variable declaration
If you declare a variable in a statement, the specific method is as follows:
<ol class="dp-c"><li class="alt"><span><span class="vars">$variable</span><span> = </span><span class="string">'value'</span><span>; </span></span></li></ol>
The compiler first finds the value of the right half of the statement (i.e. everything to the right of the equal sign). In some programming books, this is represented as the RHS (right half) of the statement. It is this part of the statement that often causes errors. If you use incorrect syntax, you will get a parsing error.
Parse error
Parse error: parse error, unexpected T_WHILE in c:program filesapache groupapachehtdocsscript.php on line 19
Before each confirmation When an error occurs, parsing errors keep appearing one after another. Because PHP stops executing the script after the first parsing error, debugging and correcting this series of errors can often be particularly tiresome.
Also, parsing errors have very little information, with almost no reporting of the line number where the error occurred. The specific reason is that when an error occurs, the compiler determines that the syntax of several lines should look valid until it encounters invalid syntax. The most likely case is that predefined words are used in the expression, such as;
<ol class="dp-c"><li class="alt"><span><span class="keyword">while</span><span> = 10; </span><span class="comment">// Bad ? while 就是一个预定义字词,不能分配给一个值</span><span> </span></span></li></ol>
Predefined words include while, function, etc. If PHP uses uses to evaluate your code. You cannot use these predefined words to name variables, And if you insist on doing this, PHP will report more errors, which you can't stand.
Regarding this issue, the following example may be helpful to you. Please read the PHP code shown below:
<ol class="dp-c"><li class="alt"><span><span class="vars">$b</span><span> = </span><span class="string">"somevalue"</span><span> </span><span class="keyword">if</span><span>(</span><span class="vars">$b</span><span> == </span><span class="string">"somevalue"</span><span>){ print </span><span class="string">"Hello world!"</span><span>; } ?> </span></span></li></ol>
The error is on the "$b=" line (missing semicolon at the end of the statement), So the error should be "parse error: missing semicolon on line 3", 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 line 4, the syntax of the if() statement is correct. So what is confusing the compiler? The clue is the "unexpected T_IF" part. When an "unexpected T_???" error occurs, it means: the compiler found that a predefined word appears in a position where it should not appear. T_IF represents if(), T_WHILE represents while(), T_FOR represents for(), etc.
Thankfully, the cause of some errors is also simple:
statements are not terminated with a semicolon (;), such as the example above. Quotation marks are missing in the string.
Some other common mistakes
The most common mistake I have seen is when not using braces ( } ) to end a function or a loop Error, this is probably the most common and annoying error. The specific code is as follows:
<ol class="dp-c"><li class="alt"><span><span class="keyword">function</span><span> UselessFunction() { </span><span class="keyword">for</span><span>(</span><span class="vars">$i</span><span> < 0; </span><span class="vars">$i</span><span> < 10; </span><span class="vars">$i</span><span>++){ } </span></span></li></ol>
The following error will be generated:
Parse error: parse error, unexpected $ in c:program filesapache groupapachehtdocsereg2 .php on line 9
Since the function UselessFunction does not end with a brace ( } ), the PHP compiler keeps looking for the closing brace until it reaches the end of the file. Because the compiler doesn't find a matching brace, it reports an end-of-file error.
如果正确地反映了代码的层次结构,错误信息就会变得非常明显。如果没有标明代码的层次结构,那么最后要想查清楚到底忘记了什么也会变得几乎是不可能的。所以,请记住,一定要标明代码的层次结构。Tab键可以很容易地实现这一点。对后续的开发人员来说,把握代码框架并对其进行修改也会更容易一些。
MySQL 错误
另一极其令人讨厌的错误信息就是最常见的MySQL错误,这常常使 PHP新手感到颇为头疼:
Warning: Supplied argument is not a valid MySQL result resource in...
上面所报告有错的一行可能是:
<ol class="dp-c"><li class="alt"><span><span class="keyword">while</span><span>(</span><span class="vars">$row</span><span> = mysql_fetch_array(</span><span class="vars">$result</span><span>)) {} </span></span></li></ol>
参数 $result并不是一个有效的资源。在英语中它表示因为查询失败,将无法处理mysql_fetch_array。任一查询的语法无效(您应该将查询复制-粘贴到MySQL 控制台参考来进行测试),或者与数据库的连接失败(这种情况下您应该再次检查用户名和口令等)。
防止错误发生
第一步,智能代码器可采取以下几步来消除下列错误出现:
结论
本文我们对PHP编译器可报出的一些看起来可能没有什么意义的错误有了一定的了解。我们需要将所学的知识应用到如何避免错误以及错误出现时如何纠正错误。调试是一个开发人员所有工作中的最重要的部分之一。提高调试效率可大大加快整个工作的进度,缩短完成一项工程所需花费的时间,同时还可以明显减轻代码失败所带来的精神压力。