Home > Article > Backend Development > Detailed explanation of Boolean judgment in PHP
In programming, many times we need to make Boolean judgments on variables. But sometimes the results of program judgment are different from what we expected. Today we will test the judgment of PHP under various circumstances.
How to judge whether the variable is empty, without further ado, just code directly
$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"]);
The result is as follows:
//isset returns false for uninitialized or null variables, indicating that they have not been initialized
//empty is null for uninitialized variables, number 0, string 0, false, empty string, will return true, indicating that the variable is empty, so empty is to force the variable to a boolean type, and then return
//is_null when the variable is null, it will return true
//var == null when the variable is null , numbers 0, false, and empty strings will all return true
//var === null When the variable is null, true will be returned
Boolean(false);
int(0)
float(0.0)
The empty string and the string "0"
are not An array containing any element
Special type NULL (including variables that have not been set)
Related recommendations:
##[Course] PHP underlying analysis video tutorial
The above is the detailed content of Detailed explanation of Boolean judgment in PHP. For more information, please follow other related articles on the PHP Chinese website!