EchoFool Posted May 27, 2010 Share Posted May 27, 2010 Is it possible to do case statements with "ranges"... So say i have the number 100 and the case checked if the number was between 50 and 150 ? Currently i can only see the case statement using precise values... if its not possible is my only other option using if statements? Quote Link to comment Share on other sites More sharing options...
ChaosKnight Posted May 27, 2010 Share Posted May 27, 2010 Try this: switch($num){ case $num > 50 && $num < 150 : //action goes here break; } Quote Link to comment Share on other sites More sharing options...
EchoFool Posted May 27, 2010 Author Share Posted May 27, 2010 Would that be more efficient than if statements? Quote Link to comment Share on other sites More sharing options...
ChaosKnight Posted May 27, 2010 Share Posted May 27, 2010 If the different values are very limited and you only have this single range, then an "if" will be a better option, if there are a lot of different ranges, then a case will be the best tool for the job... But both will work Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 27, 2010 Share Posted May 27, 2010 Yes, you can use ANY kind of comparison in a CASE statement - you just have to be creative. Remember the case value must match the switch value. So, just use the boolean true for the switch value and the comparisons as the case value Example switch(true) { case ($num>50 && $num<150): //action goes here break; case ($num>=150 && $num<300): //action goes here break; } Quote Link to comment 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.