On 13 September 2004 20:23, Gryffyn, Trevor wrote:

> Nice!  I didn't know you could do that with switch.  I was
> wondering why conditionals were left out of PHP's switch
> statement, but hoped someone would prove me wrong.  Thanks John!  
> Very helpful! 
> 
> -TG
> 
> > -----Original Message-----
> > From: John Holmes [mailto:[EMAIL PROTECTED]
> > Sent: Monday, September 13, 2004 3:26 PM
> > To: Gryffyn, Trevor; php
> > Cc: René Fournier
> > Subject: Re: [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.:
> > 
> > Something like this... it'll account for >360 degrees, too. No
> > different that a bunch of IF statements, though...
> > 
> > <?php
> > $dir = 378;
> > switch($dir = $dir%360)
> > {
> >     case 0 <= $dir && $dir < 90:
> >         echo 'northeasterly';
> >     break;
> >     case 90 <= $dir && $dir < 180:
> >         echo 'southeasterly';
> >     break;
> >     case 180 <= $dir && $dir < 270:
> >         echo 'southwesterly';
> >     break;
> >     case 270 <= $dir && $dir < 360:
> >         echo 'northwesterly';
> >     break;
> > }

That will actually give the wrong answer for $dir==0, since then the switch
value will equate to (bool)FALSE, which will fail to match case 0 <= $dir &&
$dir < 90 because that evaluates to (bool)TRUE in this situation.  I'm
guessing you'll get 'southeasterly' as that's the first FALSE value, but
theoretically you could get any one of the other values since all the other
conditions evaluate to FALSE!

If you're using this kind of condition-matching switch statement, it's
absolutely necessary to use switch(TRUE) to make it clear exactly what's
going on; so the above should be:

    $dir %= 360;
    switch (TRUE)
    {
      etc....
    }


Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

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

Reply via email to