Jump to content
Larry Ullman's Book Forums

Switch Statement With Range Of Numbers


Recommended Posts

Hi, I'm struggling with this particular use of a switch statement.

I'm trying to use a case that is a range of numbers.

Seems to work alright, until you change the variable $number to 0 (ZERO).

 

<?php
$number=3;
switch ($number) {
case $number >=0 && $number <=10:
 print "The number is between 0 and 10";
 break;

 case $number >=11 && $number <=20:
 print "The number is between 11 and 20";
 break;

 default:
 print "Your number is not between 0 and 20";
}

?>

 

Or many other forms I've tried.

 

case >=0 && <=10:

 

case ">=0 && <=10":

 

case ">=0" && "<=10":

 

case ($number >=0 && $number <=10):

 

case ($number >="0" && $number <="10"):

 

 

I guess the longer question is what is the correct way to write this idea so that it works?

I've looked at it so long I'm probably missing something real simple (like how to deal with zero as an integer), but I can't see it and any help would be appreciated greatly.

 

Many Thanks,

love the book!

Link to comment
Share on other sites

I wasn't sure about what was going on myself until I looked into the issue a bit more, but it would appear that there is an interesting caveat with the switch statement when passed a value that loosely evaluates to the Boolean false (and the integer 0 meets that condition). And when I say "loosely", think ==, not ===.

 

Essentially, switch (0) means the same thing as switch (false), and when you do that, the first case that does NOT evaluate to true is printed out. Thus, your between 11 and 20 (inclusive) statement is the one that is printed out. If you change switch ($number) to switch (true), then you will get the intended result, as the first case that evaluates to true is then output.

 

Also, the PHP.net manual has a decent description of the switch statement and this caveat.

Link to comment
Share on other sites

Thanks HartleySan. When you say php.net has a decent description are you referring to the following . . .

Note that switch/case does loose comparision.

The construct switch(true) is a little beyond the single Chapter 6 description we've been introduced to so far.

 

Back to the question of syntax, does it make more sense to compare strings than numbers with switch?

 

I feel like I've descended down a rabbit hole, but I'm sure it will all make sense eventually, for instance (and I guess this is confirming what you wrote)

<?php

$i=0;

switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
}
?>

returns "i equals 0" . . . however

<?php

$i=0;

switch ($i) {
case $i==0:
echo "i equals 0";
break;
case $i==1:
echo "i equals 1";
break;
}
?>

returns "i equals 1"

Link to comment
Share on other sites

I think of using switch anytime I have a comparison to make against multiple possible values, whether those are numeric or strings. The checking against true technique is just a handy little trick for situations like yours, where you need to compare a variable against complex conditions.

Link to comment
Share on other sites

 Share

×
×
  • Create New...