PHP中Boolean的判定詳解

*文
發布: 2023-03-18 09:52:02
原創
2908 人瀏覽過

程式設計中,很多時候我們需要對變數進行Boolean的判定。但有的時候程序判定的結果和我們預想有所差異,今天我們來測試在各種情況下PHP的判定情況。

如何判斷變數為空,話不多說,直接程式碼

       $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"]);
登入後複製

結果如下:
PHP中Boolean的判定詳解

PHP中Boolean的判定詳解

結論:
//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=>"";

相關推薦:

[課程]PHP趣味課堂

php快速上手總結

#[課程]PHP底層分析影片教學


#

以上是PHP中Boolean的判定詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板