Home php教程 php手册 PHP中判断数组是否为空的五种方法

PHP中判断数组是否为空的五种方法

Jun 06, 2016 pm 08:05 PM

本文介绍了PHP开发中遇到的数组问题,小编在这里给大家总结了5中方法关于php判断数组是否为空问题,需要的朋友参考下

本文介绍了PHP开发中遇到的数组问题,这里介绍了判断PHP数组为空的5种方法,有需要的朋友可以借鉴参考一下。

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

说明:它并不会判断变量是否为空,并且可以用来判断数组中元素是否被定义过

注意:当使用isset来判断数组元素是否被初始化过时,它的效率比array_key_exists高4倍左右

<?php
$a = &#39;&#39;;
$a[&#39;c&#39;] = &#39;&#39;;
if (!isset($a)) echo &#39;$a 未被初始化&#39; . "";
if (!isset($b)) echo &#39;$b 未被初始化&#39; . "";
if (isset($a[&#39;c&#39;])) echo &#39;$a 已经被初始化&#39; . "";
// 显示结果为
// $b 未被初始化
// $a 已经被初始化
Copy after login

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

说明:任何一个未初始化的变量、值为 0 或 false 或 空字符串”” 或 null的变量、空数组、没有任何属性的对象,都将判断为empty==true

注意1:未初始化的变量也能被empty检测为”空”

注意2:empty只能检测变量,而不能检测语句

<?php
$a = 0;
$b = &#39;&#39;;
$c = array();
if (empty($a)) echo &#39;$a 为空&#39; . "";
if (empty($b)) echo &#39;$b 为空&#39; . "";
if (empty($c)) echo &#39;$c 为空&#39; . "";
if (empty($d)) echo &#39;$d 为空&#39; . "";
Copy after login

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

说明:值为 0 或 false 或 空字符串”” 或 null的变量、空数组、都将判断为 null

注意:与empty的显著不同就是:变量未初始化时 var == null 将会报错。

<?php
$a = 0;
$b = array();
if ($a == null) echo &#39;$a 为空&#39; . "";
if ($b == null) echo &#39;$b 为空&#39; . "";
if ($c == null) echo &#39;$b 为空&#39; . "";
// 显示结果为
// $a 为空
// $b 为空
// Undefined variable: c
Copy after login

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

说明:当变量被赋值为”null”时,检测结果为true

注意1:null不区分大小写:$a = null; $a = NULL 没有任何区别

注意2:仅在变量的值为”null”时,检测结果才为true,0、空字符串、false、空数组都检测为false

注意3:变量未初始化时,程序将会报错

<?php
$a = null;
$b = false;
if (is_null($a)) echo &#39;$a 为NULL&#39; . "";
if (is_null($b)) echo &#39;$b 为NULL&#39; . "";
if (is_null($c)) echo &#39;$c 为NULL&#39; . "";
// 显示结果为
// $a 为NULL
// Undefined variable: c
Copy after login

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″等值的干扰。

以上所述是小编给大家介绍的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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles