I came from a different language, so I skipped this function in php. For example, if I want to check if there is a number 2, 4, from 7 to 10 or 15, I would like to express it as:
if ($x in [2, 4, 7...10, 15]) { do_something(); }
Instead:
if ($x == 2 || $x == 4 || ($x >= 7 && $x <= 10) || $x == 15) { do_something(); }
Or:
switch ($x) {
case 2:
case 4:
case 7:
case 8:
case 9:
case 10:
case 15:
do_something();
break;
}
Or even:
switch (true) {
case ($x == 2):
case ($x == 4):
case ($x >= 7 && $x <= 10):
case ($x == 15):
do_something();
break;
}
Is there a way in php to do this or a workaround like this? I often use this comparison in my code, and the “bundled” format makes my code more readable and less error prone (writing 7...10is safer than writing x >= 7 && x <= 10). Thank.
source
share