Home Backend Development PHP Tutorial PHP中判断变量为空的几种方法

PHP中判断变量为空的几种方法

Jun 23, 2016 pm 01:41 PM

 1. isset功能:判断变量是否被初始化

  说明:它并不会判断变量是否为空,并且可以用来判断数组中元素是否被定义过
注意:当使用isset来判断数组元素是否被初始化过时,它的效率比array_key_exists高4倍左右

  

  复制代码 代码如下:

  $a = '';
$a['c'] = '';
if (!isset($a)) echo '$a 未被初始化' . "";
if (!isset($b)) echo '$b 未被初始化' . "";
if (isset($a['c'])) echo '$a 已经被初始化' . "";
// 显示结果为
// $b 未被初始化
// $a 已经被初始化

  2. empty功能:检测变量是否为"空"

  说明:任何一个未初始化的变量、值为 0 或 false 或 空字符串"" 或 null的变量、空数组、没有任何属性的对象,都将判断为empty==true
注意1:未初始化的变量也能被empty检测为"空"
注意2:empty只能检测变量,而不能检测语句

  

  复制代码 代码如下:

  $a = 0;
$b = '';
$c = array();
if (empty($a)) echo '$a 为空' . "";
if (empty($b)) echo '$b 为空' . "";
if (empty($c)) echo '$c 为空' . "";
if (empty($d)) echo '$d 为空' . "";

  3. var == null功能:判断变量是否为"空"

  说明:值为 0 或 false 或 空字符串"" 或 null的变量、空数组、都将判断为 null
注意:与empty的显著不同就是:变量未初始化时 var == null 将会报错。

  

  复制代码 代码如下:

  $a = 0;
$b = array();
if ($a == null) echo '$a 为空' . "";
if ($b == null) echo '$b 为空' . "";
if ($c == null) echo '$b 为空' . "";
// 显示结果为
// $a 为空
// $b 为空
// Undefined variable: c

  4. is_null功能:检测变量是否为"null"

  说明:当变量被赋值为"null"时,检测结果为true
注意1:null不区分大小写:$a = null; $a = NULL 没有任何区别
注意2:仅在变量的值为"null"时,检测结果才为true,0、空字符串、false、空数组都检测为false
注意3:变量未初始化时,程序将会报错

  

  复制代码 代码如下:

  $a = null;
$b = false;
if (is_null($a)) echo '$a 为NULL' . "";
if (is_null($b)) echo '$b 为NULL' . "";
if (is_null($c)) echo '$c 为NULL' . "";
// 显示结果为
// $a 为NULL
// Undefined variable: c

  5. var === null功能:检测变量是否为"null",同时变量的类型也必须是"null"

  说明:当变量被赋值为"null"时,同时变量的类型也是"null"时,检测结果为true
注意1:在判断为"null"上,全等于和is_null的作用相同
注意2:变量未初始化时,程序将会报错

  总结PHP中,"NULL" 和 "空" 是2个概念。

  isset  主要用来判断变量是否被初始化过
empty  可以将值为 "假"、"空"、"0"、"NULL"、"未初始化" 的变量都判断为TRUE
is_null  仅把值为 "NULL" 的变量判断为TRUE
var == null  把值为 "假"、"空"、"0"、"NULL" 的变量都判断为TRUE
var === null  仅把值为 "NULL" 的变量判断为TRUE

  所以我们在判断一个变量是否真正为"NULL"时,大多使用 is_null,从而避免"false"、"0"等值的干扰。


------------------------------------------------------------------------------------------------


从数据库中取出值后判断是否为空,这个看起来很简单,只要和null比较一下就可以了,其实不然,

if($obj==null){ }
Copy after login

这样写会报错的:Notice: Trying to get property of non-object problem,

查了一下发现需要使用下面的写法

if (isset($obj)) { echo "This var is set set so I will print."; }
Copy after login

这个isset是做什么的呢? 

isset函数是检测变量是否设置。 

格式:bool isset ( mixed var [, mixed var [, ...]] ) 

返回值: 

若变量不存在则返回 FALSE 
若变量存在且其值为NULL,也返回 FALSE 
若变量存在且值不为NULL,则返回 TURE 
同时检查多个变量时,每个单项都符合上一条要求时才返回 TRUE,否则结果为 FALSE 
如果已经使用 unset() 释放了一个变量之后,它将不再是 isset()。若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE。同时要注意的是一个 NULL 字节(”\0″)并不等同于 PHP 的 NULL 常数。

警告: isset() 只能用于变量,因为传递任何其它参数都将造成解析错误。若想检测常量是否已设置,可使用 defined() 函数。 

看来刚才我那边的判断所出的问题,就是因为这个“是一个 NULL 字节(”\0″)并不等同于 PHP 的 NULL 常数”。


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)

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,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

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