Home >Backend Development >PHP Tutorial >PHP error message analysis_PHP tutorial

PHP error message analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:35:23862browse

更好地理解错误信息可以大大节省确定并改正错误内容所花费的时间。因此,在本文中,我将努力阐明多种不同类型的PHP报错信息,以及在开发过程中如何正确理解各种报错信息的含义。

编译PHP脚本时,PHP编译器会尽其所能报告它遇到的第一个问题。这样就产生一个问题:只有当错误出现时,PHP才能将它识别出来(本文后面对此问题进行了详细描述)。正是由于这个缘故,编译器指出出错的那行,从表面上看来可能语法正确无误,或者可能是根本就不存在的一行!

本文中所讲述的内容与您所应用的PHP的版本无关,因为本文所描述的各种错误并不限定于某一特殊版本的特定错误。另外我们假定您是一位初级或者中级程序员,并已经从事编程工作有半年或一年的时间。

编译器的工作方式

 要搞清楚编译器为什么会报告某一行上存在错误,首先必须明确编译器解析PHP代码的机制。我并不打算在本文中对此进行详细论述,但是,我们将会讨论一些更易于引发错误的简单概念。

变量声明

如果在一条语句中声明一个变量,具体方式如下所示:
$variable = value;
编译器首先求出语句右半部分的值(即等号右边的所有内容)。在一些编程书籍中,将此表示为语句的 RHS (右半部分)。恰恰正是语句的这一部分常常会引发错误。如果使用的语法不正确,就会出现解析错误。

解析错误

 Parse error:解析错误,unexpected T_WHILE in c:program filesapache groupapachehtdocsscript.PHP on line 19

每次确定了前一错误时,解析错误一个接一个地不断出现。因为PHP在第一个解析错误之后就停止执行脚本,调试并纠正这一系列的错误往往会让人觉得特别厌烦。

而且,解析错误具有很少的信息,几乎不报告错误所在的行号。具体原因就是当出现错误时,编译器判定好几行的语法看起来应该是有效的,直至遇到无效的语法,最可能的情形就是表达式中使用了预定义的字词,例如;while = 10; // Bad ? while 就是一个预定义字词,不能分配给一个值

预定义的字词包括 while、function等,如果PHP使用 uses to evaluate your code. 您不能使用这些预定义字词来命名变量,而且如果您非要这样做的话,PHP就会报出更多的错误,这是您无法忍受。

关于这个问题,下面的示例可能会对您有所帮助。请咨询阅读一下下面所示的PHP 代码:

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

错误位于"$b ="一行(在语句的末端缺少分号),所以错误应该是"解析错误:第3行缺少分号"对吧?而不应该依据解析器判定的:

Parse error: parse error, unexpected T_IF in c:program filesapache
groupapachehtdocsereg2.PHP on line 4
在第4行,if() 语句的语法是正确的。那么,编译器是被什么给搞糊涂了呢?线索就是"unexpected T_IF" 部分。出现 "unexpected T_???"错误时,它所表示的含义为:编译器发现在预定义字不应该出现的位置出现。T_IF 代表 if(), T_WHILE 代表 while(), T_FOR 代表 for()等。
值得庆幸的是,一些错误的原因也很简单:
语句没有使用分号(;)结束,比如上面的示例。字符串中缺少引号。

其他一些常见的错误

我见过的最常见的错误就是,当没有使用大括号( } )结束一个函数或者一个循环时出现的错误,这很可能是最常见,最让人烦的错误。具体代码如下:

function UselessFunction() {
for($i < 0; $i < 10; $i++){
}

将产生下列错误:

Parse error: parse error, unexpected $ in c:program filesapache
groupapachehtdocsereg2.PHP on line 9
由于函数 UselessFunction 没有使用大括号( } )来结束,PHP编译器不断查找表示结束的大括号直至到达文件末尾为止。因为编译器未找到一个匹配的大括号,就会报告文件末尾处有错误。

如果正确地反映了代码的层次结构,错误信息就会变得非常明显。如果没有标明代码的层次结构,那么最后要想查清楚到底忘记了什么也会变得几乎是不可能的。所以,请记住,一定要标明代码的层次结构。Tab键可以很容易地实现这一点。对后续的开发人员来说,把握代码框架并对其进行修改也会更容易一些。

MySQL 错误

Another extremely annoying error message is the most common MySQL error, which often causes headaches for new PHP users: Warning: Supplied argument is not a valid MySQL result resource in...
Reported above The wrong line may be:
while($row = MySQL_fetch_array($result)) {
The parameter $result is not a valid resource. In English it means that mysql_fetch_array cannot be processed because the query failed. The syntax of either query is invalid (you should copy-paste the query into the MySQL console reference to test), or the connection to the database failed (in which case you should double-check the username, password, etc.).

Prevent errors from happening

In the first step, the smart coder can take the following steps to eliminate the following errors:

· Don’t think about adding a semicolon at the end of every statement – ​​this should become a habit.
· Always indicate the hierarchy of your code whenever possible. This will allow you to see if you forgot to add braces at places like if calls or at the end of functions.
· Please use an editor with syntax highlighting (such as HTML-Kit). With the help of such an editor, you can determine if you forgot to add a quote, if you are missing a semicolon, etc.
Conclusion

In this article, we have a certain understanding of some seemingly meaningless errors that the PHP compiler can report. We need to apply what we learn to how to avoid mistakes and how to correct them when they occur. Debugging is one of the most important parts of a developer's job. Improving debugging efficiency can greatly speed up the progress of the entire work and shorten the time it takes to complete a project. It can also significantly reduce the mental pressure caused by code failure.
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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508346.htmlTechArticleBetter understanding of error messages can significantly save the time spent identifying and correcting erroneous content. So, in this article, I'll try to shed some light on the many different types of PHP error messages and what to expect when...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn