判断变量是否不为空,函数isset()、!empty()与!is_null()的比较

转载:https://blog.csdn.net/qq_38812954/article/details/79581785

判断变量的值,尤其是判断他们是否不为空,我们有以下4种方法:

  • if(isset($test)) true:变量已被赋值/设置
  • if(!empty($test)) true:变量不为空
  • if(!is_null($test)) true:变量不为空
  • if($test) true:以自身为参数,变量不为空

(为方便讨论,empty与is_null均取反值,使4个函数都为true时,变量不为空)

四个函数的区别,先说结论0,例子具体分析看第1部分。


0.总结isset(), !empty(), !is_null(),以自身为参数的区别

  1. isset()、!empty()会首先检查变量是否存在(存在返回true),然后再对变量值进行检测; 
    is_null()、以自身为参数,直接检查变量值是否为null,如果变量未定义会出现错误警告。
  2. isset()、!empty()的输入参数必须是一个变量($变量),因为它们是语言结构,不是函数,无法被变量函数调用(参考阅读:可变函数); 
    is_null()、以自身为参数,输入参数只要是能够有返回值的就可以(常量、变量、表达式等都可以);
  3. 判断为空的时刻: 
    • isset():仅当 未定义 或者 值为null 时,返回false;
    • !empty():未定义、 NULL、 “”(空字符)、0、“0”、FALSE、array(),均返回false;
    • !is_null():直接判断是否不为null,只有为null才返回false;未定义会出现错误警告;
    • 以自身为参数:未定义、 NULL、 “”(空字符)、0、“0”、FALSE、array(),均返回false;变量未定义时出现错误警告;

1.例子具体分析

4个函数对输入值为:数值(正常)、“”(空字符串)、array()(空数组)、0、“0”、false、null、值未定义,8种情况分别进行检验。 
测试代码如下:

  1.  
    <?php
  2.  
    $test=array("数值"=>100,"空字符串\"\""=>"","空数组array()"=>array(),"数值0"=>0,"字符\"0\""=>"0","false"=>false,"null"=>null);
  3.  
    $i=1;
  4.  
    /*将前七种情况放在数组里(最后一种是变量未定义),方便后面foreach循环测试*/
  5.  
     
  6.  
    foreach( $test as $key=>$value){
  7.  
    echo 'try:$test',$i,'=',$key,'<br/>';
  8.  
    echo 'isset',isset($value)?' 1 define':' 0 undefine','<br/>';
  9.  
    echo '!empty',!empty($value)?' 1 no empty':' 0 empty','<br/>';
  10.  
    echo '!is_null',!is_null($value)?' 1 no null':' 0 null','<br/>';
  11.  
    echo '以自身为参数',$value?' 1 no null':' 0 null','<br/>';
  12.  
    echo '<br/>';
  13.  
    ++ $i;
  14.  
    }
  15.  
    /*4个函数对前七种情况通过foreach循环进行测试输出,返回1为true,0为false。*/
  16.  
     
  17.  
    $key="值未定义";
  18.  
    unset($value);//使用unset()销毁指定的变量$value;
  19.  
    echo 'try:$test',$i,'=',$key,'<br/>';
  20.  
    echo 'isset',isset($value)?' 1 define':' 0 undefine','<br/>';
  21.  
    echo '!empty',!empty($value)?' 1 no empty':' 0 empty','<br/>';
  22.  
    echo '!is_null',!is_null($value)?' 1 no null':' 0 null','<br/>';
  23.  
    echo '以自身为参数',$value?' 1 no null':' 0 null','<br/>';
  24.  
    echo '<br/>';
  25.  
    /*对最后一种情况:变量未定义进行测试*/
  26.  
    ?>

测试结果如下:

try:$test1=数值 
isset 1 define 
!empty 1 no empty 
!is_null 1 no null 
以自身为参数 1 no null

try:$test2=空字符串”” 
isset 1 define 
!empty 0 empty 
!is_null 1 no null 
以自身为参数 0 null

try:$test3=空数组array() 
isset 1 define 
!empty 0 empty 
!is_null 1 no null 
以自身为参数 0 null

try:$test4=数值0 
isset 1 define 
!empty 0 empty 
!is_null 1 no null 
以自身为参数 0 null

try:$test5=字符”0” 
isset 1 define 
!empty 0 empty 
!is_null 1 no null 
以自身为参数 0 null

try:$test6=false 
isset 1 define 
!empty 0 empty 
!is_null 1 no null 
以自身为参数 0 null

try:$test7=null 
isset 0 undefine 
!empty 0 empty 
!is_null 0 null 
以自身为参数 0 null

try:$test8=值未定义 
isset 0 undefine 
!empty 0 empty 
!is_null 
Notice: Undefined variable: value in D:\xampp\htdocs\test\0105vs_isset_empty_is_null.php on line 22 
0 null 
以自身为参数 
Notice: Undefined variable: value in D:\xampp\htdocs\test\0105vs_isset_empty_is_null.php on line 23 
0 null

函数的true/false可用下表进行归纳(”1”表true,”0”表false):

函数/$t的值 备注 isset($t) !empty($t) !is_null($t) $t
100 有值 1 1 1 1
“” 空字符串 1 0 1 0
array() 空数组 1 0 1 0
0 数值0 1 0 1 0
“0” 字符0 1 0 1 0
false false 1 0 1 0
null null 0 0 0 0
  这里$t未定义 0 0 0(Notice) 0(Notice)

从上表可知:

  • 对于值为null和未定义的变量,四种方式都能返回false 
    • 其中,!is_null()和“以自身为参数”对于未定义的变量还会出现Notice直接报错;
  • !empty()和“以自身为参数” 还会对“”、array()、0、“0”、false,均返回false;
  • 而isset()和!is_null()只对null和未定义变量做出false判断;

isset()、!empty()的输入参数必须是一个变量

  1.  
    $test=100;
  2.  
    echo isset($test),'<br/>';
  3.  
    echo !empty($test),'<br/>';
  4.  
    echo !is_null($test),!is_null(100),!is_null($test=100),'<br/>';
    • 只有!is_null(),可以直接写!is_null(100),!is_null($b=100);

    • 而isset()和!empty()这样写会报错,输入参数只能写入一个变量($变量)

    • 因为isset()和!empty()是语言结构,is_null()是一个函数;

猜你喜欢

转载自www.cnblogs.com/xcwytu/p/10921984.html