php中,isset,empty,is_null区别

php中,empty和is_null,isset这几个函数挺让人迷糊的,在项目中也经常使用,于是写了个小示例,加深下印象

$testCase = array(
    1 => '',
    2 => "",
    3 => null,
    4 => FALSE,
    5 => NULL,
    6 =>'0',
    7 =>0,
    8 => " ",
    9 => TRUE,
    
);
echo "<br><br>empty test";
foreach ($testCase as $k => $v) {
    if (empty($v)) {
        echo "<br> $k=>$v is empty";
    }
}
echo "<br><br>isset test";
foreach ($testCase as $k => $v) {
    if (isset($v)) {
        echo "<br> $k=>$v is set";
    }
}

echo "<br><br>is_null test";
foreach ($testCase as $k => $v) {
    if (is_null($v)) {
        echo "<br> $k=>$v is null";
    }
}

输出结果如下:



empty test
1=> is empty
2=> is empty
3=> is empty
4=> is empty
5=> is empty
6=>0 is empty
7=>0 is empty

isset test
1=> is set
2=> is set
4=> is set
6=>0 is set
7=>0 is set
8=> is set
9=>1 is set

is_null test
3=> is null
5=> is null

从结果中可以看出
empty认为0,'0',false,null等都是为空
isset认为null,NULL为未定义
is_null认为null,NULL为真,其它为假。

posted @ 2018-09-06 16:43  随彦心MO  阅读(96)  评论(0)    收藏  举报