为了账号安全,请及时绑定邮箱和手机立即绑定

PHP判断变量为空的方法及PHP中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"]);

结果如下:
图片描述
图片描述

结论:
//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=>"";

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
5
获赞与收藏
452

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消