boolean vs int comparison

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I always have doubts about comparison between boolean and integers.
Lets PHP shows hows is that he does.

The results where:
false <   $int === true / $int !== 0
false <=  $int === true
false >   $int === false
false >=  $int === false / $int !== 0
false ==  $int === false / $int !== 0
false === $int === false
false !=  $int === true / $int !== 0
false !== $int === true
true  <   $int === false
true  <=  $int === true / $int !== 0
true  >   $int === false / $int !== 0
true  >=  $int === true
true  ==  $int === true / $int !== 0
true  === $int === false
true  !=  $int === false / $int !== 0
true  !== $int === true

<?php # the script I used

function bol2string($boolean)
{
     return $boolean ? 'true' : 'false';
}

$results = array();

foreach(array(false, true) as $boolean)
    foreach(array('<  ', '<= ', '>  ', '>= ', '== ', '===', '!= ', '!==') as
$test)
        for($i=-1; $i<1; ++$i)
        {
            @eval(
                '$value =' . (
                    $_test = bol2string($boolean) . ' ' . $test . ' ' . $i
                ) . ';'
            );
            $results[ $boolean][ $test ][ $i ] = $value;
        }

foreach($results as $boolean => $_boolean)
    foreach($_boolean as $test => $_test)
        echo bol2string($boolean) , $boolean ? '  ': ' ' ,
            $test ,' $int === ' , bol2string($_test[-1]),
            $_test[0] !== $_test[-1] ? ' / $int !== 0' : '' ,
        PHP_EOL;
?>
-- 
Martin Scotta

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux