PHP - Get bool to echo false when false

前端 未结 14 1689
予麋鹿
予麋鹿 2020-11-22 14:09

The following code doesn\'t print out anything:

$bool_val = (bool)false;
echo $bool_val;

But the following code prints 1:

相关标签:
14条回答
  • 2020-11-22 14:53
    function dump_condition($condition){
        if($condition){
            return "true";
        } else {
            return "false";
        }
     }
    

    use on script

    echo dump_condition(1>0); // print "true"
    
    echo dump_condition(1<0); // print "false"
    
    0 讨论(0)
  • 2020-11-22 14:54

    Your'e casting a boolean to boolean and expecting an integer to be displayed. It works for true but not false. Since you expect an integer:

    echo (int)$bool_val;
    
    0 讨论(0)
提交回复
热议问题