Why is 0 greater than 2?


#1



PHP 6. All On Your Own!


Why when I set $i as 0 it returns as "greater than 2"?
The code behaves properly with all other integers.
Also any other string also returns as "less than 2" rather than the default case.


<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
    <?php
     $i = 0;
     switch ($i) {
         case $i > 2:
             echo "the number is greater than 2";
             break;
         case $i < 2:
            echo "the number is less than 2";
            break;
        case $i == 2:
            echo "the number is 2";
            break;
        default:
            echo "that is not a number";
            
     }
    ?>
    </body>
</html>


#2

@tagblaster69397,
Try it with

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
    <?php
     $i = 0;
     switch (true) {
         case $i > 2:
             echo "the number is greater than 2";
             break;
         case $i < 2:
            echo "the number is less than 2";
            break;
        case $i == 2:
            echo "the number is 2";
            break;
        default:
            echo "that is not a number";
            
     }
    ?>
    </body>
</html>