Switch and Case between ranges in PHP
Sometimes you may want to use a switch statement with specific ranges. It’s doable. All you have to do is place true
inside the switch and then check the conditions inside the cases.
<?php
switch (true) {
case ($number < 10):
// do something
break;
case ($number < 100):
// do something
break;
default:
// do something
}
?>
Example:
<?php
$yourVariable = 150;
switch (true) {
case ($yourVariable < 100):
echo 'less than 100';
break;
case ($yourVariable < 200):
echo 'less than 200';
break;
default:
echo '200 or more';
}
?>
Output: less than 200