So here is the code –
$info = 0;
switch ( $info ) {
case ( $info < 11 ):
$ts = "zero";
break;
case ( $info <= 44 && $info >= 11):
$ts = "two";
break;
case ( $info > 44 ):
$ts = "three";
break;
}
echo $ts;
It doesn’t give out zero, bur it gives out “two”, maybe you have an idea why? It only happens if $info = 0.
It’s because
caseexpects a value, not a condition, and the conditions you’re providing are being converted into values before comparison with theswitchargument.With
$infobeing 0:$info < 11is true, hence gives 1.$info <= 44 && $info >= 11is false, hence gives 0.$info > 44is false, hence gives 0.So it’s matching the first case where the
$infovalue of 0 is equal to the condition converted to a number, which is the second one. That’s why you’re seeing"two".I would suggest changing it into an
if/elsevariant:or possibly better, since it removes extraneous checks and ensures that some value is always assigned: