判断变量是否不为空,函数isset()、!empty()与!is_null()的比较

本文介绍了PHP中检测变量是否已定义及非空的四种方法:isset()、!empty()、!is_null()及直接使用变量作为条件。通过实例对比了这些方法在不同变量值下的表现,帮助开发者选择最适合的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

判断变量的值,尤其是判断他们是否不为空,我们有以下4种方法:

  • if(isset($test)) true:变量已被赋值/设置
  • if(!empty($test)) true:变量不为空
  • if(!is_null($test)) true:变量不为空
  • if($test) true:以自身为参数,变量不为空

(为方便讨论,empty与is_null均取反值,使4个函数都为true时,变量不为空)

四个函数的区别,先说结论0,例子具体分析看第1部分。


0.总结isset(), !empty(), !is_null(),以自身为参数的区别

  1. isset()、!empty()会首先检查变量是否存在(存在返回true),然后再对变量值进行检测; 
    is_null()、以自身为参数,直接检查变量值是否为null,如果变量未定义会出现错误警告。
  2. isset()、!empty()的输入参数必须是一个变量($变量),因为它们是语言结构,不是函数,无法被变量函数调用(参考阅读:可变函数); 
    is_null()、以自身为参数,输入参数只要是能够有返回值的就可以(常量、变量、表达式等都可以);
  3. 判断为空的时刻: 
    • isset():仅当 未定义 或者 值为null 时,返回false;
    • !empty():未定义、 NULL、 “”(空字符)、0、“0”、FALSE、array(),均返回false;
    • !is_null():直接判断是否不为null,只有为null才返回false;未定义会出现错误警告;
    • 以自身为参数:未定义、 NULL、 “”(空字符)、0、“0”、FALSE、array(),均返回false;变量未定义时出现错误警告;

1.例子具体分析

4个函数对输入值为:数值(正常)、“”(空字符串)、array()(空数组)、0、“0”、false、null、值未定义,8种情况分别进行检验。 
测试代码如下:

<?php
    $test=array("数值"=>100,"空字符串\"\""=>"","空数组array()"=>array(),"数值0"=>0,"字符\"0\""=>"0","false"=>false,"null"=>null);
    $i=1;
    /*将前七种情况放在数组里(最后一种是变量未定义),方便后面foreach循环测试*/

    foreach( $test as $key=>$value){
        echo 'try:$test',$i,'=',$key,'<br/>';
        echo 'isset',isset($value)?' 1 define':' 0 undefine','<br/>';
        echo '!empty',!empty($value)?' 1 no empty':' 0 empty','<br/>';
        echo '!is_null',!is_null($value)?' 1 no null':' 0 null','<br/>';
        echo '以自身为参数',$value?' 1 no null':' 0 null','<br/>';
        echo '<br/>';
        ++$i;  
    }
    /*4个函数对前七种情况通过foreach循环进行测试输出,返回1为true,0为false。*/

        $key="值未定义";
        unset($value);//使用unset()销毁指定的变量$value;
        echo 'try:$test',$i,'=',$key,'<br/>';
        echo 'isset',isset($value)?' 1 define':' 0 undefine','<br/>';
        echo '!empty',!empty($value)?' 1 no empty':' 0 empty','<br/>';
        echo '!is_null',!is_null($value)?' 1 no null':' 0 null','<br/>';
        echo '以自身为参数',$value?' 1 no null':' 0 null','<br/>';
        echo '<br/>';
        /*对最后一种情况:变量未定义进行测试*/
?>
测试结果如下:

try:$test1=数值 
isset 1 define 
!empty 1 no empty 
!is_null 1 no null 
以自身为参数 1 no null

try:$test2=空字符串”” 
isset 1 define 
!empty 0 empty 
!is_null 1 no null 
以自身为参数 0 null

try:$test3=空数组array() 
isset 1 define 
!empty 0 empty 
!is_null 1 no null 
以自身为参数 0 null

try:$test4=数值0 
isset 1 define 
!empty 0 empty 
!is_null 1 no null 
以自身为参数 0 null

try:$test5=字符”0” 
isset 1 define 
!empty 0 empty 
!is_null 1 no null 
以自身为参数 0 null

try:$test6=false 
isset 1 define 
!empty 0 empty 
!is_null 1 no null 
以自身为参数 0 null

try:$test7=null 
isset 0 undefine 
!empty 0 empty 
!is_null 0 null 
以自身为参数 0 null

try:$test8=值未定义 
isset 0 undefine 
!empty 0 empty 
!is_null 
Notice: Undefined variable: value in D:\xampp\htdocs\test\0105vs_isset_empty_is_null.php on line 22 
0 null 
以自身为参数 
Notice: Undefined variable: value in D:\xampp\htdocs\test\0105vs_isset_empty_is_null.php on line 23 
0 null

函数的true/false可用下表进行归纳(”1”表true,”0”表false):

函数/$t的值 备注 isset($t) !empty($t) !is_null($t) $t
100 有值 1 1 1 1
“” 空字符串 1 0 1 0
array() 空数组 1 0 1 0
0 数值0 1 0 1 0
“0” 字符0 1 0 1 0
false false 1 0 1 0
null null 0 0 0 0
  这里$t未定义 0 0 0(Notice) 0(Notice)

从上表可知:

  • 对于值为null未定义的变量,四种方式都能返回false 
    • 其中,!is_null()“以自身为参数”对于未定义的变量还会出现Notice直接报错
  • !empty()和“以自身为参数” 还会对“”、array()、0、“0”、false,均返回false
  • isset()和!is_null()只对null和未定义变量做出false判断

isset()、!empty()的输入参数必须是一个变量

$test=100;
echo isset($test),'<br/>';
echo !empty($test),'<br/>';
echo !is_null($test),!is_null(100),!is_null($test=100),'<br/>';
  • 只有!is_null()可以直接写!is_null(100),!is_null($b=100)

  • isset()和!empty()这样写会报错,输入参数只能写入一个变量($变量)

  • 因为isset()和!empty()是语言结构,is_null()是一个函数;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值