如何判断变量为空,话不多说,直接代码
$testIsset ="赋值了";
$results = array();
array_push($results,["state"=>"变量被赋值","testFun"=>"isset()","result"=>isset($testIsset),"resultDes"=>"isset返回true"]);
array_push($results,["state"=>"变量被赋值","testFun"=>"empty()","result"=>empty($testIsset),"resultDes"=>"empty返回false"]);
array_push($results,["state"=>"变量被赋值","testFun"=>"is_null()","result"=>is_null($testIsset),"resultDes"=>"is_null返回false"]);
array_push($results,["state"=>"变量被赋值","testFun"=>"var == null","result"=>$testIsset==null,"resultDes"=>"var == null返回false"]);
array_push($results,["state"=>"变量被赋值","testFun"=>"var === null","result"=>$testIsset===null,"resultDes"=>"var === null返回false"]);
unset($testIsset);
array_push($results,["state"=>"变量unset后","testFun"=>"isset()","result"=>isset($testIsset),"resultDes"=>"isset返回false"]);
array_push($results,["state"=>"变量unset后","testFun"=>"empty()","result"=>empty($testIsset),"resultDes"=>"empty返回true"]);
array_push($results,["state"=>"变量unset后","testFun"=>"is_null()","result"=>"","resultDes"=>"函数调用时报错"]);
array_push($results,["state"=>"变量unset后","testFun"=>"var == null","result"=>"","resultDes"=>"函数调用时报错"]);
array_push($results,["state"=>"变量unset后","testFun"=>"var === null","result"=>"","resultDes"=>"函数调用时报错"]);
$testIsset = null;
array_push($results,["state"=>"变量被设置为null","testFun"=>"isset()","result"=>isset($testIsset),"resultDes"=>"isset返回false"]);
array_push($results,["state"=>"变量被设置为null","testFun"=>"empty()","result"=>empty($testIsset),"resultDes"=>"empty返回true"]);
array_push($results,["state"=>"变量被设置为null","testFun"=>"is_null()","result"=>is_null($testIsset),"resultDes"=>"is_null返回true"]);
array_push($results,["state"=>"变量被设置为null","testFun"=>"var == null","result"=>$testIsset==null,"resultDes"=>"var == null返回true"]);
array_push($results,["state"=>"变量被设置为null","testFun"=>"var === null","result"=>$testIsset===null,"resultDes"=>"var === null返回true"]);
$testIsset = 0;
array_push($results,["state"=>"变量被设置为数字0","testFun"=>"isset()","result"=>isset($testIsset),"resultDes"=>"isset返回true"]);
array_push($results,["state"=>"变量被设置为数字0","testFun"=>"empty()","result"=>empty($testIsset),"resultDes"=>"empty返回true"]);
array_push($results,["state"=>"变量被设置为数字0","testFun"=>"is_null()","result"=>is_null($testIsset),"resultDes"=>"is_null返回false"]);
array_push($results,["state"=>"变量被设置为数字0","testFun"=>"var == null","result"=>$testIsset==null,"resultDes"=>"var == null返回true"]);
array_push($results,["state"=>"变量被设置为数字0","testFun"=>"var === null","result"=>$testIsset===null,"resultDes"=>"var === null返回false"]);
$testIsset = "0";
array_push($results,["state"=>"变量被设置为字符串0","testFun"=>"isset()","result"=>isset($testIsset),"resultDes"=>"isset返回true"]);
array_push($results,["state"=>"变量被设置为字符串0","testFun"=>"empty()","result"=>empty($testIsset),"resultDes"=>"empty返回true"]);
array_push($results,["state"=>"变量被设置为字符串0","testFun"=>"is_null()","result"=>is_null($testIsset),"resultDes"=>"is_null返回false"]);
array_push($results,["state"=>"变量被设置为字符串0","testFun"=>"var == null","result"=>$testIsset==null,"resultDes"=>"var == null返回false"]);
array_push($results,["state"=>"变量被设置为字符串0","testFun"=>"var === null","result"=>$testIsset===null,"resultDes"=>"var === null返回false"]);
$testIsset = false;
array_push($results,["state"=>"变量被设置为false","testFun"=>"isset()","result"=>isset($testIsset),"resultDes"=>"isset返回true"]);
array_push($results,["state"=>"变量被设置为false","testFun"=>"empty()","result"=>empty($testIsset),"resultDes"=>"empty返回true"]);
array_push($results,["state"=>"变量被设置为false","testFun"=>"is_null()","result"=>is_null($testIsset),"resultDes"=>"is_null返回false"]);
array_push($results,["state"=>"变量被设置为false","testFun"=>"var == null","result"=>$testIsset==null,"resultDes"=>"var == null返回true"]);
array_push($results,["state"=>"变量被设置为false","testFun"=>"var === null","result"=>$testIsset===null,"resultDes"=>"var === null返回false"]);
$testIsset = "";
array_push($results,["state"=>"变量被设置为空字符串","testFun"=>"isset()","result"=>isset($testIsset),"resultDes"=>"isset返回true"]);
array_push($results,["state"=>"变量被设置为空字符串","testFun"=>"empty()","result"=>empty($testIsset),"resultDes"=>"empty返回true"]);
array_push($results,["state"=>"变量被设置为空字符串","testFun"=>"is_null()","result"=>is_null($testIsset),"resultDes"=>"is_null返回false"]);
array_push($results,["state"=>"变量被设置为空字符串","testFun"=>"var == null","result"=>$testIsset==null,"resultDes"=>"var == null返回true"]);
array_push($results,["state"=>"变量被设置为空字符串","testFun"=>"var === null","result"=>$testIsset===null,"resultDes"=>"var === null返回false"]);
结果如下:
结论:
//isset对于未初始化或者为null的变量返回false,表明未被初始化
//empty对于未被初始化,为null,数字0,字符串0,false,空字符串,都会返回true,表示变量是空的,所以empty就是将变量强转为boolean类型,然后返回
//is_null当变量为null时会返回true
//var == null当变量为null,数字0,false,空字符串时都会返回true
//var === null当变量为null是才会返回true
PHP中Boolean类型详解
-
当转换为 boolean 时,以下值被认为是 FALSE;
Boolean(false);
int(0)
float(0.0)
空字符串及字符串"0"
不包括任何元素的数组
特殊类型 NULL (包括尚未设定的变量) - 当转换为 boolean 时,以下值被认为是 FALSE;
布尔转字符串时,true=>1,false=>"";