The cleanest looking multiple "if" scenario is to use a "Switch" statement.  
Unfortunately I don't believe PHP's "switch" will do varied conditions, only equality 
statements:

$j = 5;
switch ($j) {
  case "< 6":
    echo "first";
    break;
  case <6:
    echo "second";
    break;
  case 5:
    echo "third";
    break;
  default:
    echo "fourth";
    break;
}


This breaks at "case <6".  Removing the second condition, the switch statement echos 
"third" since $j == 5.

In some other languages, you could put your range of values in the "case" statements, 
but not PHP I guess.

I think in this case, you're stuck with a bunch of if/elseif statements.  Only 16 of 
them though, right? :)

-TG

> -----Original Message-----
> From: René Fournier [mailto:[EMAIL PROTECTED] 
> Sent: Friday, July 16, 2004 6:20 PM
> To: php
> Subject: [PHP] convert degrees to heading
> 
> 
> I have to write a little function to convert a direction from degrees 
> to a compass -type heading. 0 = West. 90 = North. E.g.:
> 
> from:
> 135 degrees
> 
> to:
> NW
> 
> Now, I was planning to write a series of if statements to 
> evaluate e.g.,
> 
> if ($heading_degrees < 112.5 && $heading_degrees > 67.5) {
>       $heading_compass = "N";
>       }
> 
> The works, but it will require
> 
> N, NNW, NNE, NE, NW, ENE, NWW... many IF statements.
> 
> Can anyone think of a programatically more elegant and 
> efficient way of 
> converting this type of data? (I suppose this is not really a 
> problem, 
> just a curiosity.)
> 
> ...Rene
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to