Jump to content

[SOLVED] Switch statement bug? when value < 0


mmosel

Recommended Posts

Ok check this out. I've seen this error twice in my code. Is it a bug or am I totally missing something? When I parse the following statement, on both my live and local servers, it echos that $value (when 0 ) is greater than 300. If I switch $value = to 1 then it's fine. So why the problem when = 0? I'm running php 5 on both servers.

 

$value = 0;
switch ($value){
  case $value < 301:
    echo "less than 301";
  break;
  case $value > 300:
    echo "greater than 300";
  break;
}

outputs: greater than 300

as far as i am aware switch statements can only be used with specific values your switching make no sense.

basically switch statements work like:

say you have

switch ($value){

  case $value < 301:

    echo "less than 301";

  break;

now when it gets to the case it make like a if that looks like

if($value == $value < 301){

//do case

}

so it wont work

i can't explain why you get any output i would think it would skip anything all together but you need to use if-else statements

 

Scott.

In order to do what the OP wants, the value that is switched on has to be "true":

<?php
$value = 0;
switch (true){
  case ($value < 301):
    echo "less than 301";
  break;
  case ($value > 300):
    echo "greater than 300";
  break;
}
?>

 

Ken

but a switch on true might as well be a if statement

as there is no real difference between

<?php 
$value = 0;
switch (true){
  case ($value < 301):
    echo "less than 301";
  break;
  case ($value > 300):
    echo "greater than 300";
  break;
}
?>
and
<?php
$value = 0;
if ($value < 301){
    echo "less than 301";
  }else if($value > 300){
    echo "greater than 300";
}
?>

only that the second one is easier to read and is shorter

 

Scott.

 

This took me a while to figure this one out, What I THINK is happening is this..

 

when $value = 0, $value < 301 is returning true and $value > 300 is returning false, so the switch statement ends up like this...

 

<?php
$value = 0;
switch ($value) {
case true:
	echo "less than 301";
	break;
case false:
	echo "greater than 300";
	break;
}

 

But 0 is another way of saying "false" (every other non-zero integer is true), so it ends up following the false case because as far as php is concerned $value is false, so it matches the case conditional.

 

Do this..

 

<?php
$value = 0;
switch (true) {
case ($value < 301):
	echo "less than 301";
	break;
case ($value > 300):
	echo "greater than 300";
	break;
}

 

And everything should function as expected ;)

 

Edit: just explaining the reason you need to put true instead of $value

Interesting. I've used it for ranges on a few occasions and it works great! I've seen examples of it elsewhere as well. People have posted similar examples on php.net, but it's not an official example. I noticed that on php.net it says switch/case does 'loose comparison', so perhaps that's the answer. When 0, it's probably evaluating to false and causing the problem.

 

I know I can use if else statements. But for a whole boatload of ranges, it's a nice trick to use the switch. It does work, except for the 0 value. Give it a try...

 

-----

 

update - I like generic's solution - it makes sense since it is simply checking for true or false. I've seen this example on php.net also.

update - I like generic's solution - it makes sense since it is simply checking for true or false. I've seen this example on php.net also.

 

Do note that other people gave the same solution before me. I just explained my best guess as to why you need to do it this way.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.