好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

PHP empty() isset() is_null() 区别与性能比较 - php函数

PHP empty() isset() is_null() 区别与性能比较

在php中empty() isset() is_null()三个函数都是判断是否为空的情况,但是如果我个要具体的深入去了解这个三个函数发现还是有许多的区别.

is_null(), empty(), isset(),这几个函数以及 == ],== array() 会在实际操作中经常用到,因为功能很类似,可能会忽视了他们的区别,一不小心就会给工作带来很大的麻烦,下面将这几种结构列出来,供自己和大家参考,鉴于表述的准确性,部分解释来自英文原版手册,避免中文手册的更新不及时以及翻译不当等问题。

is_null()

is_null(),bool,当参数满足 null 的三种情况时,is_null() 将返回 TRUE。

null类型,以下情况将被认定为 NULL:

it has been assigned the constant NULL.

it has not been set to any value yet.

it has been unset().

source:http://cn2.php.net/manual/en/language.types.null.php

isset()

isset(),bool,用于判定参数是否被设定并且不是 NULL。参数只能是变量。

如果没有设置变量,或者变量被 unset() 掉,或者变量值为 NULL ,返回 FALSE,其它情况返回 TRUE。即如果不是 NULL 就属于 isset 的范畴了,这一点和 is_null() 函数正好相反。

如果传递多个参数,将取交集。即所有参数全部符合 isset() 时才返回 TRUE。

ps:defined(),bool,用于检查常量是否被设置。

source:http://cn2.php.net/manual/en/function.isset.php

empty()

empty(),bool,主要用于判断变量是否为空,参数只能是变量。

如下情况将被判定位空,代码如下:

[] (an empty string)

0 (0 as an integer)

0.0 (0 as a float)

[0″ (0 as a string)

NULL

FALSE

array() (an empty array)

var $var; (a variable declared, but without a value in a class)

注: 如果参数是未设置的变量,变量将被认定为 NULL,不会报错,返回 TRUE。

但是注意在 5.0.0 之后,Objects with no properties are no longer considered empty.

source:http://cn2.php.net/manual/en/function.empty.php

判定是否为空的方式还有 ==],== array() 等,比较有局限性,都没什么好说的,测试的类型如下:

<?php  $a ;  $b  = false;  $c  =  '' ;  $d  = 0;  $e  = null;  $f  =  array ();  ?> 

empty()

首先是empty的var_dump输出:

<?php  var_dump( empty empty ( $a ));  var_dump( empty empty ( $b ));  var_dump( empty empty ( $c ));  var_dump( empty empty ( $d ));  var_dump( empty empty ( $e ));  var_dump( empty empty ( $f ));  ?>  /*   程序输出为:   bool(true)   bool(true)   bool(true)   bool(true)   bool(true)   bool(true)   */  

从代码中可以看出,只要数据类型是否为空或假,empty()就输出true。

isset()

再看看isset的输出:

var_dump(isset( $a ));  var_dump(isset( $b ));  var_dump(isset( $c ));  var_dump(isset( $d ));  var_dump(isset( $e ));  var_dump(isset( $f ));  /* 输出   bool(false)   bool(true)   bool(true)   bool(true)   bool(false)   bool(true)   */  

可以看出isset()只能用来判断是否为NULL和未定义。

is_null()

最后是is_null的输出:

var_dump( is_null ( $a ));  var_dump( is_null ( $b ));  var_dump( is_null ( $c ));  var_dump( is_null ( $d ));  var_dump( is_null ( $e ));  var_dump( is_null ( $f ));  /*输出   bool(true)   bool(false)   bool(false)   bool(false)   bool(true)   bool(false)   */  

is_null 字面意思了。

由此可见 empty() 可以用来判定所有的数据类型是否为空或假,而 is_null 与 isset 基本一样,只能用来判断是否为NULL和未定义。

概括总结isset,empty,is_null区别:

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

<?php  $a =100;  $b = "" ;  $c =null;  //isset检查   echo   "isset" , "$a=$a" ,isset( $a )? "define" : "undefine" , "rn" ;  echo   "isset" , "$b=$b" ,isset( $b )? "define" : "undefine" , "rn" ;  echo   "isset" , "$c=$c" ,isset( $c )? "define" : "undefine" , "rn" ;  unset( $b );  echo   "isset" , "$b" ,isset( $b )? "define" : "undefine" , "rn" ;  $b =0;  echo   "rnrn" ;  //empty检查   echo   "empty" , "$a=$a" ,! empty empty ( $a )? "no empty" : "empty" , "rn" ;  echo   "empty" , "$b=$b" ,! empty empty ( $b )? "no empty" : "empty" , "rn" ;  echo   "empty" , "$c=$c" ,! empty empty ( $c )? "no empty" : "empty" , "rn" ;  unset( $b );  echo   "empty" , "$b" ,! empty empty ( $b )? "no empty" : "empty" , "rn" ;  $b =0;  echo   "rnrn" ;  //is_null检查   echo   "is_null" , "$a=$a" ,! is_null ( $a )? "no null" : "null" , "rn" ;  echo   "is_null" , "$b=$b" ,! is_null ( $b )? "no null" : "null" , "rn" ;  echo   "is_null" , "$c=$c" ,! is_null ( $c )? "no null" : "null" , "rn" ;  unset( $b );  echo   "is_null" , "$b" , is_null ( $b )? "no null" : "null" , "rn" ; 

查看更多关于PHP empty() isset() is_null() 区别与性能比较 - php函数的详细内容...

  阅读:37次