views:

51

answers:

1

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

+3  A: 

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";
}
gnarf