php empty,isset,is_null比较(差异与异同)_转

做php开发时候,想必在使用:empty,isset,is_null  这几个函数时候,遇到一些问题。甚至给自己的程序带来一些安全隐患的bug。很多时候,对于isset,empty都认为差不多。因此开发时候,就没有注 意,一段作为流程判断时候,就出现bug问题了。

 

 

一、举例说明

A.一个变量没有定义,我们该怎么样去判断呢?

 

01 <?php
02 #不存在 $test 变量
03  
04 $isset = isset( $test )? "test is define!" : "test is undefine!" ;
05 echo "isset:$isset\r\n" ;
06  
07 $empty =! empty ( $test )? "test is define!" : "test is undefine!" ;
08 echo "empty:$empty\r\n" ;
09  
10 $is_null = is_null ( $test )? "test is define!" : "test is undefine!" ;
11 echo "is_null:$is_null\r\n" ;

 

测试结果是:

image

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

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

isset函数参数:

<?php
$test=100;
echo isset($test),isset(100),$isset($b=100);

 

<br />
<b>Parse error</b>:  parse error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '$' in <b>PHPDocument3</b> on line <b>3</b><br />

empty函数参数:

<?php
$test=100;

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

 

<br />
<b>Parse error</b>:  parse error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '$' in <b>PHPDocument3</b> on line <b>3</b><br />

is_null函数参数:

<?php
$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个函数进行比较文章。很少涉及这些。下面我要说的,是在都检查已存在变量情况下,不同之处。

实例:

01 <?php
02 $a =100;
03 $b = "" ;
04 $c =null;
05 //isset检查
06 echo "isset" , "\$a=$a" ,isset( $a )? "define" : "undefine" , "\r\n" ;
07 echo "isset" , "\$b=$b" ,isset( $b )? "define" : "undefine" , "\r\n" ;
08 echo "isset" , "\$c=$c" ,isset( $c )? "define" : "undefine" , "\r\n" ;
09 unset( $b );
10 echo "isset" , "\$b" ,isset( $b )? "define" : "undefine" , "\r\n" ;
11 $b =0;
12 echo "\r\n\r\n" ;
13  
14 //empty检查
15 echo "empty" , "\$a=$a" ,! empty ( $a )? "no empty" : "empty" , "\r\n" ;
16 echo "empty" , "\$b=$b" ,! empty ( $b )? "no empty" : "empty" , "\r\n" ;
17 echo "empty" , "\$c=$c" ,! empty ( $c )? "no empty" : "empty" , "\r\n" ;
18 unset( $b );
19 echo "empty" , "\$b" ,! empty ( $b )? "no empty" : "empty" , "\r\n" ;
20 $b =0;
21 echo "\r\n\r\n" ;
22  
23 //is_null检查
24 echo "is_null" , "\$a=$a" ,! is_null ( $a )? "no null" : "null" , "\r\n" ;
25 echo "is_null" , "\$b=$b" ,! is_null ( $b )? "no null" : "null" , "\r\n" ;
26 echo "is_null" , "\$c=$c" ,! is_null ( $c )? "no null" : "null" , "\r\n" ;
27 unset( $b );
28 echo "is_null" , "\$b" , is_null ( $b )? "no null" : "null" , "\r\n" ;

 

image

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

empty

如果 变量 是非空或非零的值,则 empty() 返回 FALSE 。换句话说,""、0、"0"、NULLFALSE 、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 . 哈哈,这个问题怎么样解决呢?等待大家分享……

Tag标签: php
0
0
0
(请您对文章做出评价)
« 上一篇: php通过文件头检测文件类型通用类(zip,rar…)
» 下一篇: web程序乱码深入分析【基础原理篇】--php为例
posted on 2010-10-18 12:46 程默 阅读(237) 评论(1) 编辑 收藏 所属分类: php


评论

1936986

#1楼 [ 楼主 ] 2010-10-18 13:29 程默        
先抛一个砖,检查变量存在,并且值为NULL.
01 <?php
02 function checkNull( $a )
03 {
04      if ( array_key_exists ( $a , $GLOBALS ))
05      {
06          global $ $a ;
07          if ( is_null ($ $a ))
08          return true;
09      }
10      return   false;
11 }
12 $test =null;
13 var_dump(checkNull( "test" ));
14 var_dump(checkNull( "test1" ));
  回复   引用   查看    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值