Table of Contents
盘点PHP编程常见失误,盘点php编程
  变量声明
  解析错误
  常见的错误
  MySQL错误
  注意echo和print的区别
  注意空字符串('')和NULL的区别
  分清include与require的区别
  注意isset和empty的区别
  分清self::和this-->的区别
Home php教程 php手册 盘点PHP编程常见失误,盘点php编程

盘点PHP编程常见失误,盘点php编程

Jun 13, 2016 am 09:12 AM
php exist common Developer hour Overview inventory coding programming

盘点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,那么就使用指向操作符->。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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

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

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 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 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

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

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

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.

See all articles