PHP error message analysis_PHP tutorial
更好地理解错误信息可以大大节省确定并改正错误内容所花费的时间。因此,在本文中,我将努力阐明多种不同类型的PHP报错信息,以及在开发过程中如何正确理解各种报错信息的含义。 编译PHP脚本时,PHP编译器会尽其所能报告它遇到的第一个问题。这样就产生一个问题:只有当错误出现时,PHP才能将它识别出来(本文后面对此问题进行了详细描述)。正是由于这个缘故,编译器指出出错的那行,从表面上看来可能语法正确无误,或者可能是根本就不存在的一行! 本文中所讲述的内容与您所应用的PHP的版本无关,因为本文所描述的各种错误并不限定于某一特殊版本的特定错误。另外我们假定您是一位初级或者中级程序员,并已经从事编程工作有半年或一年的时间。 编译器的工作方式 要搞清楚编译器为什么会报告某一行上存在错误,首先必须明确编译器解析PHP代码的机制。我并不打算在本文中对此进行详细论述,但是,我们将会讨论一些更易于引发错误的简单概念。 变量声明 如果在一条语句中声明一个变量,具体方式如下所示: 解析错误 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" 错误位于"$b ="一行(在语句的末端缺少分号),所以错误应该是"解析错误:第3行缺少分号"对吧?而不应该依据解析器判定的: Parse error: parse error, unexpected T_IF in c:program filesapache 其他一些常见的错误 我见过的最常见的错误就是,当没有使用大括号( } )结束一个函数或者一个循环时出现的错误,这很可能是最常见,最让人烦的错误。具体代码如下: function UselessFunction() { 将产生下列错误: Parse error: parse error, unexpected $ in c:program filesapache 如果正确地反映了代码的层次结构,错误信息就会变得非常明显。如果没有标明代码的层次结构,那么最后要想查清楚到底忘记了什么也会变得几乎是不可能的。所以,请记住,一定要标明代码的层次结构。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... 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. 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.
$variable = value;
编译器首先求出语句右半部分的值(即等号右边的所有内容)。在一些编程书籍中,将此表示为语句的 RHS (右半部分)。恰恰正是语句的这一部分常常会引发错误。如果使用的语法不正确,就会出现解析错误。
if($b == "somevalue"){
print "Hello world!";
}
?>
groupapachehtdocsereg2.PHP on line 4
在第4行,if() 语句的语法是正确的。那么,编译器是被什么给搞糊涂了呢?线索就是"unexpected T_IF" 部分。出现 "unexpected T_???"错误时,它所表示的含义为:编译器发现在预定义字不应该出现的位置出现。T_IF 代表 if(), T_WHILE 代表 while(), T_FOR 代表 for()等。
值得庆幸的是,一些错误的原因也很简单:
语句没有使用分号(;)结束,比如上面的示例。字符串中缺少引号。
for($i < 0; $i < 10; $i++){
}
groupapachehtdocsereg2.PHP on line 9
由于函数 UselessFunction 没有使用大括号( } )来结束,PHP编译器不断查找表示结束的大括号直至到达文件末尾为止。因为编译器未找到一个匹配的大括号,就会报告文件末尾处有错误。
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.).
· 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
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
