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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.