papaface Posted December 28, 2007 Share Posted December 28, 2007 Hello, I know that switches can be used to check the subject for a value e.g: switch ($value) { case "2": //do something break; } ^ That does something if $value is "2". But how do you this to check if $value is more than 10 for instance? Quote Link to comment https://forums.phpfreaks.com/topic/83477-solved-switch-operators/ Share on other sites More sharing options...
Daniel0 Posted December 28, 2007 Share Posted December 28, 2007 You can't. Quote Link to comment https://forums.phpfreaks.com/topic/83477-solved-switch-operators/#findComment-424680 Share on other sites More sharing options...
papaface Posted December 28, 2007 Author Share Posted December 28, 2007 You can't. I didn't think so. Any reason why you can't? Quote Link to comment https://forums.phpfreaks.com/topic/83477-solved-switch-operators/#findComment-424684 Share on other sites More sharing options...
Daniel0 Posted December 28, 2007 Share Posted December 28, 2007 Switches are only used to check if something equals something else. That's just the way switches are. You'll have to use an if structure to use any other operators. Quote Link to comment https://forums.phpfreaks.com/topic/83477-solved-switch-operators/#findComment-424686 Share on other sites More sharing options...
kenrbnsn Posted December 28, 2007 Share Posted December 28, 2007 I beg to differ: <?php $val = rand(0,30); switch (true) { case ($val == 0): echo 'Value (' . $val . ') is zero'; break; case $val > 0 && $val < 20: echo 'Value (' . $val . ') is between 0 and 20'; break; case ($val >= 20): echo 'Value (' . $val . ') is greater than or equal to 20'; break; } echo "<br>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/83477-solved-switch-operators/#findComment-424691 Share on other sites More sharing options...
papaface Posted December 28, 2007 Author Share Posted December 28, 2007 I beg to differ: <?php $val = rand(0,30); switch (true) { case ($val == 0): echo 'Value (' . $val . ') is zero'; break; case $val > 0 && $val < 20: echo 'Value (' . $val . ') is between 0 and 20'; break; case ($val >= 20): echo 'Value (' . $val . ') is greater than or equal to 20'; break; } echo "<br>\n"; ?> Ken Thanks. I was just reading through the switch documentation and read that it can be done using your method. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/83477-solved-switch-operators/#findComment-424692 Share on other sites More sharing options...
Daniel0 Posted December 28, 2007 Share Posted December 28, 2007 I beg to differ: <?php $val = rand(0,30); switch (true) { case ($val == 0): echo 'Value (' . $val . ') is zero'; break; case $val > 0 && $val < 20: echo 'Value (' . $val . ') is between 0 and 20'; break; case ($val >= 20): echo 'Value (' . $val . ') is greater than or equal to 20'; break; } echo "<br>\n"; ?> Ken Hmm, didn't know you could do that, but there is no reason to do that. The point of the switch, from what I see it, is that you don't have to repeat the variable name you are checking all the time. Quote Link to comment https://forums.phpfreaks.com/topic/83477-solved-switch-operators/#findComment-424696 Share on other sites More sharing options...
papaface Posted December 28, 2007 Author Share Posted December 28, 2007 I beg to differ: <?php $val = rand(0,30); switch (true) { case ($val == 0): echo 'Value (' . $val . ') is zero'; break; case $val > 0 && $val < 20: echo 'Value (' . $val . ') is between 0 and 20'; break; case ($val >= 20): echo 'Value (' . $val . ') is greater than or equal to 20'; break; } echo "<br>\n"; ?> Ken Hmm, didn't know you could do that, but there is no reason to do that. The point of the switch, from what I see it, is that you don't have to repeat the variable name you are checking all the time. Yeah, if's are better. But I asked if you can do it, not "is it better to use ifs" lol. Quote Link to comment https://forums.phpfreaks.com/topic/83477-solved-switch-operators/#findComment-424697 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.