盘点PHP编程常见失误,盘点php编程
盘点PHP编程常见失误,盘点php编程
概述:本文盘点PHP开发者在编码时,容易忽略或不注意引起的小失误与错误。
变量声明
如果在一条语句中声明一个变量,如下所示:$var='value';编译器首先会求出语句右半部分的值,恰恰正是语句的这一部分常常会引发错误。如果使用的语法不正确,就会出现解析错误。
解析错误
例如,Parse error:解析错误,unexpected T_WHILE in c:program filesapache groupapachehtdocsscript.php on line 19每次确定了前一错误时,解析错误一个接一个地不断出现,PHP在第一个解析错误之后就停止执行脚本。而且,解析错误具有很少的信息,几乎不报告错误所在的行号。比如表达式中使用了预定义的关键字,例如:while=10;while就是一个预定义的关键字,不能分配给它一个值。预定义关键字包括while、function等,我们不能使用这些预定义关键字来命名变量,否则编译器就会报错。其中,T_IF代表if(),T_WHILE代表while(),T_FOR代表for()等。
常见的错误
还有一些常见的错误,比如语句没有使用分号(;)结束,字符串中缺少引号等。另外就是,没有使用大括号(})结束一个函数或者一个循环,比如:function UselessFunction(){for($iphp on line 9由于函数UselessFunction没有使用大括号(})来结束,PHP编译器会不断查找表示结束的大括号直至到达文件末尾为止。因为编译器未找到一个匹配的大括号,就会报告文件末尾处有错误。如果正确地反映了代码的层次结构,错误信息就会变得非常明显。否则,代码调试起来就会非常的困难。所以,一定要标明代码的层次结构,这对后续的开发人员来说,改进代码也会更容易一些。
MySQL错误
另一类的错误信息就是MySQL错误,这常常使PHP新手感到颇为头疼,比如:Warning:Supplied argument is not a valid MySQL result resource in...上面所报告有错的一行可能是:while($row=mysql_fetch_array($result)){}参数$result并不是一个有效的资源,因为查询失败,将无法处理mysql_fetch_array。任一查询的语法无效或者与数据库的连接失败,应该到MySQL控制台进行测试。
注意echo和print的区别
PHP中echo和print都是输出的作用,但是两者之间还是有细微的差别。echo输出后没有返回值,但print有返回值,当其执行失败时返回flase。因此可以作为一个普通函数来使用,例如执行$r=print"Hello World";变量$r的值将为1。而且代码中echo语句的运行效率要略快于print语句。
注意空字符串('')和NULL的区别
PHP中空字符串和NULL都是以值为0存储的,但是他们的类型并不一样,前者是string,而后者是NULL,可见字符串('')、NULL值相等但类型不等。
分清==(等于)和===(全等于)的区别
两者都属于比较运算符,==(等于)只比较值是否相等,而===(全等于)则不但比较值是否相等,还会比较类型是否相等,它更为严格。
分清include与require的区别
include()与require()的功能也基本相同,但在用法上也有一些不同,include()是有条件包含函数,而require()则是无条件包含函数。例如在下面代码中,如果变量$a为真,则将包含文件a.php:if($a){include("a.php");}而require()则和include()不同,不管$a取何值,下面的代码都会把文件a.php包含:if($a){require("a.php");}在错误处理方面,使用include语句,如果发生包含错误,程序将跳过include语句,虽然会显示错误信息但是程序还是会继续执行。但是,requre语句会提示一个致命错误。
注意isset和empty的区别
empty是判断一个变量是否为“空”,而isset则是判断一个变量是否已经被设置。
分清self::和this-->的区别
在访问PHP类中的成员变量或方法时,如果被引用的变量或者方法被声明成const(常量)或者static(静态属性),那么就必须使用域操作符::,而如果被引用的变量或者方法没有被声明成const或者static,那么就使用指向操作符->。

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

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

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.
