php: '0' as a string with empty()

前端 未结 13 791
自闭症患者
自闭症患者 2020-12-06 04:05

I want a 0 to be considered as an integer and a \'0\' to be considered as a string but empty() considers the \'0\' as a string in the example below,

$var = \         


        
13条回答
  •  遥遥无期
    2020-12-06 04:46

    You cannot make it. That is how it was designed. Instead you can write an and statement to test:

    if (empty($var) && $var !== '0') {
        echo $var . ' is empty';
    }
    

    You could use isset, unless of course, you want it to turn away the other empties, that empty checks for.

    Edit

    Fixed the check to do a type check as well, thanks to the 2371 for pointing that out :)

提交回复
热议问题