Home php教程 php手册 php empty,isset,is

php empty,isset,is

Jun 21, 2016 am 08:56 AM
echo empty isset null test

一、举例说明
A.一个变量没有定义,我们该怎么样去判断呢

复制代码 代码如下:


#不存在$test 变量

$isset= isset($test)?"test is define!":"test is undefine!";
echo "isset:$isset\r\n";

$empty=!empty($test)?"test is define!":"test is undefine!";
echo "empty:$empty\r\n";

$is_null=is_null($test)?"test is define!":"test is undefine!";
echo "is_null:$is_null\r\n";


测试结果是:

结果出来了:empty,isset首先都会检查变量是否存在,然后对变量值进行检测。而is_null 只是直接检查变量值,是否为null,因此如果变量未定义就会出现错误!

B、看下各自接收的参数是什么?

isset函数参数:

$test=100;
echo isset($test),isset(100),$isset($b=100);



Parse error: parse error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or "$" in PHPDocument3 on line 3

empty函数参数:

$test=100;

echo empty($test),empty(100),empty($b=100);



Parse error: parse error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or "$" in PHPDocument3 on line 3

is_null函数参数:

$test=100;

echo is_null($test),is_null(100),is_null($b=100);

运行结果:没有任何错误。

比较结果出来了:empty,isset输入参数必须是一个变量(php变量是以$字符开头的),而is_null输入参数只要是能够有返回值就可以。(常量,变量,表达式等)。在php手册里面,对于他们解析是:empty,isset 是一个语言结构而非函数,因此它无法被变量函数调用。

二、概括总结isset,empty,is_null区别:
刚才介绍的:检查变量,以及参数类型,这个是这3个函数不同之处的基础,也是最容易被忽视的。看到网上有很多对这个3个函数进行比较文章。很少涉及这些。下面我要说的,是在都检查已存在变量情况下,不同之处。

实例:

复制代码 代码如下:


$a=100;
$b="";
$c=null;
//isset检查
echo "isset","\$a=$a",isset($a)?"define":"undefine","\r\n";
echo "isset","\$b=$b",isset($b)?"define":"undefine","\r\n";
echo "isset","\$c=$c",isset($c)?"define":"undefine","\r\n";
unset($b);
echo "isset","\$b",isset($b)?"define":"undefine","\r\n";
$b=0;
echo "\r\n\r\n";

//empty检查
echo "empty","\$a=$a",!empty($a)?"no empty":"empty","\r\n";
echo "empty","\$b=$b",!empty($b)?"no empty":"empty","\r\n";
echo "empty","\$c=$c",!empty($c)?"no empty":"empty","\r\n";
unset($b);
echo "empty","\$b",!empty($b)?"no empty":"empty","\r\n";
$b=0;
echo "\r\n\r\n";

//is_null检查
echo "is_null","\$a=$a",!is_null($a)?"no null":"null","\r\n";
echo "is_null","\$b=$b",!is_null($b)?"no null":"null","\r\n";
echo "is_null","\$c=$c",!is_null($c)?"no null":"null","\r\n";
unset($b);
echo "is_null","\$b",is_null($b)?"no null":"null","\r\n";



通过上面这个简单测试,我们可以大体知道,当一个变量存在情况下:isset,empty,is_null检测,得到值情况了。上面没有举例更多变量。其实测试发现:

empty

如果 变量 是非空或非零的值,则 empty() 返回 FALSE。换句话说,""、0、"0"、NULL、FALSE、array()、var $var、未定义; 以及没有任何属性的对象都将被认为是空的,如果 var 为空,则返回 TRUE。

isset

如果 变量 存在(非NULL)则返回 TRUE,否则返回 FALSE(包括未定义)。变量值设置为:null,返回也是false;unset一个变量后,变量被取消了。注意,isset对于NULL值变量,特殊处理。

is_null

检测传入值【值,变量,表达式】是否是null,只有一个变量定义了,且它的值是null,它才返回TRUE . 其它都返回 FALSE 【未定义变量传入后会出错!】.


疑问:怎么样判断一个变量被设置了,并且值为NULL呢?

通过上面比较,估计大家与我一样,会有这个问题浮现在脑海里面。 检测一个变量是否是null 可以用:is_null,但如果变量未定义用它检测会出错。因此,我们想到,检测变量是否定义可以用:isset,但是如果一个变量值是:null, 则它会返回false . 哈哈,这个问题怎么样解决呢?等待大家分享……

检查变量存在,并且值为NULL.

复制代码 代码如下:


function checkNull($a)
{
if(array_key_exists($a,$GLOBALS))
{
global $$a;
if(is_null($$a))
return true;
}
return false;
}
$test=null;
var_dump(checkNull("test"));
var_dump(checkNull("test1"));



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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Huawei Watch GT 5 smartwatch gets update with new features Huawei Watch GT 5 smartwatch gets update with new features Oct 03, 2024 am 06:25 AM

Huawei is rolling out software version 5.0.0.100(C00M01) for the Watch GT 5 and the Watch GT 5 Prosmartwatchesglobally. These two smartwatches recently launched in Europe, with the standard model arriving as the company’s cheapest model. This Harmony

Tekken\'s Colonel Sanders dream fried by KFC Tekken\'s Colonel Sanders dream fried by KFC Oct 02, 2024 am 06:07 AM

Katsuhiro Harada, the Tekken series director, once seriously tried to bring Colonel Sanders into the iconic fighting game. In an interview with TheGamer, Harada revealed that he pitched the idea to KFC Japan, hoping to add the fast-food legend as a g

Cybertruck FSD reviews praise quick lane switching and full-screen visualizations Cybertruck FSD reviews praise quick lane switching and full-screen visualizations Oct 01, 2024 am 06:16 AM

Tesla is rolling out the latest Full Self-Driving (Supervised) version 12.5.5 and with it comes the promised Cybertruck FSD option at long last, ten months after the pickup went on sale with the feature included in the Foundation Series trim price. F

Garmin releases Adventure Racing activity improvements for multiple smartwatches via new update Garmin releases Adventure Racing activity improvements for multiple smartwatches via new update Oct 01, 2024 am 06:40 AM

Garmin is ending the month with a new set of stable updates for its latest high-end smartwatches. To recap, the company released System Software 11.64 to combat high battery drain across the Enduro 3, Fenix E and Fenix 8 (curr. $1,099.99 on Amazon).

New Xiaomi Mijia Graphene Oil Heater with HyperOS arrives New Xiaomi Mijia Graphene Oil Heater with HyperOS arrives Oct 02, 2024 pm 09:02 PM

Xiaomi will shortly launch the Mijia Graphene Oil Heater in China. The company recently ran a successful crowdfunding campaign for the smart home product, hosted on its Youpin platform. According to the page, the device has already started to ship to

First look: Leaked unboxing video of upcoming Anker Zolo 4-port 140W wall charger with display First look: Leaked unboxing video of upcoming Anker Zolo 4-port 140W wall charger with display Oct 01, 2024 am 06:32 AM

Earlier in September 2024, Anker's Zolo 140W charger was leaked, and it was a big deal since it was the first-ever wall charger with a display from the company. Now, a new unboxing video from Xiao Li TV on YouTube gives us a first-hand look at the hi

Samsung Galaxy Z Fold Special Edition revealed to land in late October as conflicting name emerges Samsung Galaxy Z Fold Special Edition revealed to land in late October as conflicting name emerges Oct 01, 2024 am 06:21 AM

The launch of Samsung's long-awaited 'Special Edition' foldable has taken another twist. In recent weeks, rumours about the so-called Galaxy Z Fold Special Edition went rather quiet. Instead, the focus has shifted to the Galaxy S25 series, including

Manjaro 24.1 \'Xahea\' launches with KDE Plasma 6.1.5, VirtualBox 7.1, and more Manjaro 24.1 \'Xahea\' launches with KDE Plasma 6.1.5, VirtualBox 7.1, and more Oct 02, 2024 am 06:06 AM

With a history of over one decade, Manjaro is regarded as one of the most user-friendly Linux distros suitable for both beginners and power users, being easy to install and use. Mostly developed in Austria, Germany, and France, this Arch-based distro

See all articles