Home Backend Development PHP Tutorial Analysis of situations that cause PHP code errors_PHP Tutorial

Analysis of situations that cause PHP code errors_PHP Tutorial

Jul 20, 2016 am 10:57 AM
php people code analyze exist cause Condition of write parse still mistake

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

参数 $result并不是一个有效的资源。在英语中它表示因为查询失败,将无法处理mysql_fetch_array。任一查询的语法无效(您应该将查询复制-粘贴到MySQL 控制台参考来进行测试),或者与数据库的连接失败(这种情况下您应该再次检查用户名和口令等)。

防止错误发生

第一步,智能代码器可采取以下几步来消除下列错误出现:

  • 在每一条语句的末尾处,不必考虑添加分号——这应该成为一种习惯。
  • 总是要尽可能标明代码的层次结构,这可以使您能够查看是否忘记在if 调用或函数末端等位置添加大括号。
  • 请使用可突出显示语法的编辑器(如 HTML-Kit)。有了这类编辑器的辅助,您就能确定是否忘记了添加引号,是否缺少分号等。

结论

本文我们对PHP编译器可报出的一些看起来可能没有什么意义的错误有了一定的了解。我们需要将所学的知识应用到如何避免错误以及错误出现时如何纠正错误。调试是一个开发人员所有工作中的最重要的部分之一。提高调试效率可大大加快整个工作的进度,缩短完成一项工程所需花费的时间,同时还可以明显减轻代码失败所带来的精神压力。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445737.htmlTechArticle再小心谨慎的人,在编写程序时,还是会犯错的。下面主要介绍 PHP 中的这些小错误的情况分析。 这些错误通常会迷惑PHP编译器。如果开发...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

See all articles