Implementing ATAN2

An implementation of ATAN2 in PHP. PHP implements ATAN2 but I needed to figure out the algorithm for a microcontroller that didn't, so I prototyped it in PHP.

Here is the code:

class Math {
  public static function atan2( $dy, $dx ) {
    if( $dy > 0 ) {
      if( $dx > 0  ) $tcl = atan($dy/$dx);
      if( $dx < 0  ) $tcl = M_PI - atan(-$dy/$dx);
      if( $dx == 0 ) $tcl = M_PI/2;
    }

    if( $dy < 0 ) {
      if( $dx > 0  ) $tcl = -atan(-$dy/$dx);
      if( $dx < 0  ) $tcl = atan($dy/$dx) - M_PI;
      if( $dx == 0 ) $tcl = -M_PI/2;
    }

    if( $dy == 0 ) {
      if( $dx > 0  ) $tcl = 0.0;
      if( $dx < 0  ) $tcl = M_PI;
      if( $dx == 0 ) $tcl = 0.0; // the 2 points are the same, default to zero
    }

    return $tcl;
  }
}
And here is an example of how to use it:
$heading = 270.0;  // degrees
$Ax =  0.00;
$Ay =  0.00;
$Bx = -7.89;
$By = -0.86;

// get target distance and bearing in degrees
$dx = $Bx-$Ax;
$dy = $By-$Ay;
$dist = sqrt( pow($dx,2) + pow($dy,2) );
$dir = Math::atan2( $dy, $dx ) * 180 / M_PI;
printf( "Target spotted at %0.3f degrees, distance %0.3f !!\n", $dir, $dist );

// get new heading
$diff = $heading - $dir;
if( $diff < -180 ) $diff += 360;
if( $diff > 180 ) $diff -= 360;
printf( "Turn %s %0.3f degrees\n", ($diff < 0 ? 'left' : 'right'), $diff );