The example below is an extráct from http://php.net/manual/de/control-structures.switch.php
<?php
$totaltime = 0;
switch ($totaltime) {
case ($totaltime < 1):
echo "That was fast!";
break;
case ($totaltime > 1):
echo "Not fast!";
break;
case ($totaltime > 10):
echo "That's slooooow";
break;
}
?>
I expected the result as “That was fast.” But actual result is “Not fast!”. It would be great if some one can explain me why?
But if i add another case, case 0: echo "That was super fast!". Then it is echoing properly. i.e “That was super fast!”. Please help me how to use conditional switch statement.
EDIT:-
Thanks all for your responses. I am able to overcome the above problem by modifyong switch($totaltime) to switch(1)
case ($totaltime < 1):means1to PHP (that equation returns true)case ($totaltime > 1):means0to PHP (that equation returns false)Since
$totaltimeis 0, you get that outputIn other words PHP compares
$totaltimeto the result of the comparisons.EDIT regarding EDIT in OP:
You need to get rid of the
switch()-statement. You only use it to easily compare against different values and not use additional expressions with it.I mean what is wrong with
EDIT: please note that I switched the last two if-statements to make it really work.