Goose87 Posted March 31, 2008 Share Posted March 31, 2008 Hey everyone, I have the following switch function. It should take the value, and if its below 10, the person gets 10 quantity, and for every 10 above that, the quantity goes down by 1. The problem I have is that when the value is 0, the quantity gained goes for the quantity 9 one. I cant explain why it isnt working. switch($value){ case($value<11 OR $value==0): $quantity=10; break; case($value>=11 AND $value<20): $quantity=9; break; case($value>=20 AND $value<30): $quantity=8; break; case($value>=30 AND $value<40): $quantity=7; break; case($value>=40 AND $value<50): $quantity=6; break; case($value>=50 AND $value<60): $quantity=5; break; case($value>=60 AND $value<70): $quantity=4; break; case($value>=70 AND $value<80): $quantity=3; break; case($value>=80 AND $value<90): $quantity=2; break; case($value>=90 AND $value<100): $quantity=1; break; case 100: $quantity=0; break; } I've tried having no brackets, ive tried just $value<11. I've tried everything, but when $value is 0 it always gives a quantity of 9. Any ideas?? Thanks. Link to comment https://forums.phpfreaks.com/topic/98815-switch-statement-not-working/ Share on other sites More sharing options...
rhodesa Posted March 31, 2008 Share Posted March 31, 2008 I don't think you can use AND and OR in the cases. You can just use IF/ELSEIF/ELSE: <?php if($value >= 100) $quantity=0; elseif($value >= 90) $quantity = 1; elseif($value >= 80) $quantity = 2; elseif($value >= 70) $quantity = 3; elseif($value >= 60) $quantity = 4; elseif($value >= 50) $quantity = 5; elseif($value >= 40) $quantity = 6; elseif($value >= 30) $quantity = 7; elseif($value >= 20) $quantity = 8; elseif($value >= 10) $quantity = 9; else $quantity = 10; ?> Or, better yet, turn it into a formula: <?php $quantity = 10 - ($value / 10); ?> Link to comment https://forums.phpfreaks.com/topic/98815-switch-statement-not-working/#findComment-505661 Share on other sites More sharing options...
Goose87 Posted March 31, 2008 Author Share Posted March 31, 2008 Ah, thats a darn good point, just do the formula one i feel really stupid now Thanks a lot for your help. Link to comment https://forums.phpfreaks.com/topic/98815-switch-statement-not-working/#findComment-505668 Share on other sites More sharing options...
kenrbnsn Posted March 31, 2008 Share Posted March 31, 2008 It's perfectly fine to have a condition as a case, you just have to switch on the value "true": <?php $value = rand(0,100); switch (true) { case ($value<11): case ($value==0): $quantity=10; break; case ($value>=11 && $value<20): $quantity=9; break; case ($value>=20 && $value<30): $quantity=8; break; case ($value>=30 && $value<40): $quantity=7; break; case ($value>=40 && $value<50): $quantity=6; break; case ($value>=50 && $value<60): $quantity=5; break; case ($value>=60 && $value<70): $quantity=4; break; case ($value>=70 && $value<80): $quantity=3; break; case ($value>=80 && $value<90): $quantity=2; break; case ($value>=90 && $value<100): $quantity=1; break; default: $quantity=0; break; } echo "$value<br>$quantity<br>"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/98815-switch-statement-not-working/#findComment-505779 Share on other sites More sharing options...
rhodesa Posted March 31, 2008 Share Posted March 31, 2008 ah...interesting...i've never thought of using a switch statement like that Link to comment https://forums.phpfreaks.com/topic/98815-switch-statement-not-working/#findComment-505789 Share on other sites More sharing options...
Goose87 Posted April 7, 2008 Author Share Posted April 7, 2008 Thanks for the reply ken, that's really informative. I went down the "formula" route, it added a TINY bit of limitation to boundaries I had to set, but I don't mind. I will definately use that type of switch statement in the future, thanks again Link to comment https://forums.phpfreaks.com/topic/98815-switch-statement-not-working/#findComment-511459 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.