Simpler range finder?
-
08-07-2019 - |
Question
Hay guys
I've programmed a very simple range finder.
The user can only select numbers 1 - 180 (axis)
if the number is 90 or below i have to add 90 on to it if the number is 91 - 180 i have to take off 90 from it.
Here's what i have
$min_range = range(1,90);
$max_range = range(91,180);
if(in_array($axis, $min_range)){
$c = $axis + 90;
}elseif(in_array($axis, $max_range)){
$c = $axis - 90;
}
Has anyone got a better solution
Solution
Rather than store a huge array 1..90 and then test, why not just do an if based on less/greater than your data points?
if ($axis >= 1 && $axis <= 90) {
$c = $axis + 90;
} else if ($axis > 90 && $axis <=180) {
$c = $axis - 90;
} else {
echo "Invalid input";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow