Home > Backend Development > PHP Tutorial > Detailed explanation of Boolean judgment in PHP

Detailed explanation of Boolean judgment in PHP

*文
Release: 2023-03-18 09:52:02
Original
2948 people have browsed it

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"]);
Copy after login

The result is as follows:
Detailed explanation of Boolean judgment in PHP

Detailed explanation of Boolean judgment in PHP

## Conclusion:

//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

Detailed explanation of Boolean type in PHP

When When converted to boolean, the following values ​​are considered FALSE;

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)

When converted to boolean, the following values ​​are considered FALSE;

When Boolean is converted to a string, true=>1, false=>"";

Related recommendations:

[Course]PHP Fun Classroom

php quick start summary

##[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template