mmosel Posted March 24, 2009 Share Posted March 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/150824-solved-switch-statement-bug-when-value-0/ Share on other sites More sharing options...
ratcateme Posted March 24, 2009 Share Posted March 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150824-solved-switch-statement-bug-when-value-0/#findComment-792343 Share on other sites More sharing options...
kenrbnsn Posted March 24, 2009 Share Posted March 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/150824-solved-switch-statement-bug-when-value-0/#findComment-792345 Share on other sites More sharing options...
ratcateme Posted March 24, 2009 Share Posted March 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150824-solved-switch-statement-bug-when-value-0/#findComment-792349 Share on other sites More sharing options...
genericnumber1 Posted March 24, 2009 Share Posted March 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/150824-solved-switch-statement-bug-when-value-0/#findComment-792351 Share on other sites More sharing options...
mmosel Posted March 24, 2009 Author Share Posted March 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150824-solved-switch-statement-bug-when-value-0/#findComment-792357 Share on other sites More sharing options...
genericnumber1 Posted March 24, 2009 Share Posted March 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150824-solved-switch-statement-bug-when-value-0/#findComment-792369 Share on other sites More sharing options...
mmosel Posted March 24, 2009 Author Share Posted March 24, 2009 Oh yes, thank you kenrbnsn - you did give the solution first. I was writing my reply as a you all posted. Thanks to everyone to replied! Quote Link to comment https://forums.phpfreaks.com/topic/150824-solved-switch-statement-bug-when-value-0/#findComment-792380 Share on other sites More sharing options...
mmosel Posted March 24, 2009 Author Share Posted March 24, 2009 Oh yes, thank you kenrbnsn - you did give the solution first. I was writing my reply as you all posted. Thanks to everyone who* replied! Ugh, typo hell. Quote Link to comment https://forums.phpfreaks.com/topic/150824-solved-switch-statement-bug-when-value-0/#findComment-792394 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.