开发者

PHP and fancy boolean

Havin开发者_Python百科g some problems with booleans in php, would be very grateful if anyone could explain why the following doesnt work

$var = "asdas";

    // or this if(!isset($var) || ($var && !$var)){
    if(!isset($var) || (is_bool($var) != true && is_bool($var) != false)){
        echo "is not a boolean";
    }else{
        echo "it IS a boolean";
    }

the output is always -> "it IS a boolean"

Thanks in advance


Thanks for your help, yea I realise what I've done (school boy error).

Solution

$var = "asda";
if(!isset($var) || (is_bool($var) === false)){
    echo "it not a boolean";
}else{
    echo "is IS a boolean"; 
}


The problem is here:

(is_bool($var) != true && is_bool($var) != false)

is_bool() returns either true or false so you are eliminating the first echo.

Change your code to this:

$var = "asdas";

if(isset($var) and is_bool($var)){
    echo "it IS a boolean";
}else{
    echo "is not a boolean";
}


(is_bool($var) != true && is_bool($var) != false)

What this does:

Statement should return true ONLY if $var is NOT a boolean AND $var IS a boolean.

As you can imagine, $var can not NOT be a boolean and be a boolean at the same time. (Seems like $var is having some kind of personality disorder :))


is_bool($var) is always either true or false.

In other words, (is_bool($var) != true && is_bool($var) != false) will always evaluate to true false.


(!isset($var) || (is_bool($var) != true && is_bool($var) != false))

(!isset($var) || (!is_bool($var) && is_bool($var)))

(!isset($var) || false)

$var is set so the result is always false.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜