isset、empty、var==null、is_null、var===null详细理解

//isset: 判断变量是否被初始化
//它并不会判断变量是否为空,并且可能用来判断数组中元素是否被定义
//听说在数组用isset与array_key_exists高出4倍
$a = "";
$ar['c'] = "";
if(!isset($a)){
    echo '$a not isset\r\n';
}else{
    echo '$a isset\r\n';
}
if(!isset($b)){
    echo '$b not isset\r\n';
}else{
    echo '$b isset\r\n';
}
if(isset($ar['c'])){
    echo "ar[c] isset";
}
echo "<BR><BR>";


//empty功能:检测变量是否为"空"
//任何一个未初始化的变量,值为0或false 或空字符串"" 或null的变量,空数组,没有任何属性的对象,都将判断为empty==true
//1 未初始化的变量也能被empty检测为"空"
//2 empty()只能检测变量,而不能检测语句
$a = "";
$b = 0;
$c = array();
$d = false;
if(empty($a)) echo "var a is empty.";
if(empty($b)) echo "var b is empty.";
if(empty($c)) echo "var c is empty.";
if(empty($d)) echo "var d is empty.";
if(empty($e)) echo "var e is empty.";
echo "<BR><BR>";


//var == null 功能:判断变量是否为"空"
//说明:值为0或者false或空字符串""或null的变量,空数组,都将判断为null
//与empty的显著不同就是:变量未初始化时var == null将会报错
$a = '';
$b = false;
$d = 0;
if($a==null) echo "var a is null.";
if($b==null) echo "var b is null.";
if($d==null) echo "var d is null.";
//if($e==null) echo "var e is null.";//会报错


//is_null功能:检测变量是否为"null"
//当变量被赋值为"null"时,检测结果为true
//1 null不区分大小写
//2 仅在变量的值为null时,检测结果才为true,0、空字符串、false、空数组都检测为false
echo "<BR><BR>";
$a = 0;
if(is_null($a)) 
     echo "var a is null.";
else
    echo "var a is not null.";


//var ===null功能:检测变量是否为"null",同时变量的类型也必须是"null"
//当变量被赋值为null时,同时变量类型也是null时,检测结果为true
//在判断为null时,全等于和is_null的作用相同
//变量未初始化时,程序将报错
echo "<BR><BR>";
$a = null;
if($a===null) echo "var a is null.";

  

你可能感兴趣的:(null)