Q:

PHP bugs with comparison operator

I know the comparison operator of PHP is not 100% because of the automatic type declarations.
I did some tests and found this code
<?php
function foo($answer) {
    if ($answer > 10) {
        return true;
    } else {
        return $answer;
    }
}
if (foo(11)) {
    echo "11 is bigger than 10<br />";
}        
if (foo(9)) {
    echo "9 is bigger than 10<br />";
}
?>

The output is:
11 is bigger than 10
9 is bigger than 10
Can someone explain me where and why the code fails in this comparison.

A:

Because return $answer is truthy in both of your test cases.
This evaluates to:
if (11) { ...
if (9) { ...

A:

Function foo() always returns some value. In this case both boolean and int type. Just make sure you are more strictly when you compare returned value.
<?php
function foo($answer) {
    if ($answer > 10) {
        return true;
    } else {
        return $answer;
    }
}

if (foo(11) === true) {
   // This returned TRUE 
   echo "11 is bigger than 10<br />";
}

if ( is_int(foo(9)) === true ) {
   //This returned int 
   echo "9 is bigger than 10<br />";
}
?>

