title: PHP - distance between two locations, approach
body: <p>according to : <a href="http://sgowtham.net/ramblings/2009/08/04/php-calculating-distance-between-two-locations-given-their-gps-coordinates/" rel="nofollow">http://sgowtham.net/ramblings/2009/08/04/php-calculating-distance-between-two-locations-given-their-gps-coordinates/</a></p>
  
  <p>i'm gonna check distance between 2 given locations.</p>
  
  <p>My dubt is which of these two approaches is "better" and why if?</p>
  
  <p>Fisrt:</p>
  
  <pre><code>&lt;?php
  
  $earth_radius = 3960.00; # in miles
  $lat_1 = "47.117828";
  $lon_1 = "-88.545625";
  $lat_2 = "47.122223";
  $lon_2 = "-88.568781";
  $delta_lat = $lat_2 - $lat_1 ;
  $delta_lon = $lon_2 - $lon_1 ;
  
  # Spherical Law of Cosines
  function distance_slc($lat1, $lon1, $lat2, $lon2) {
    global $earth_radius;
    global $delta_lat;
    global $delta_lon;
    $distance  = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($delta_lon)) ;
    $distance  = acos($distance);
    $distance  = rad2deg($distance);
    $distance  = $distance * 60 * 1.1515;
    $distance  = round($distance, 4);
  
    return $distance;
  }
  
  $slc_distance = distance_slc($lat_1, $lon_1, $lat_2, $lon_2);
  ?&gt;
  </code></pre>
  
  <p>Second</p>
  
  <pre><code>&lt;?php
  
  $earth_radius = 3960.00; # in miles
  $lat_1 = "47.117828";
  $lon_1 = "-88.545625";
  $lat_2 = "47.122223";
  $lon_2 = "-88.568781";
  $delta_lat = $lat_2 - $lat_1 ;
  $delta_lon = $lon_2 - $lon_1 ;
  
  function distance_haversine($lat1, $lon1, $lat2, $lon2) {
    global $earth_radius;
    global $delta_lat;
    global $delta_lon;
    $alpha    = $delta_lat/2;
    $beta     = $delta_lon/2;
    $a        = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
    $c        = asin(min(1, sqrt($a)));
    $distance = 2*$earth_radius * $c;
    $distance = round($distance, 4);
  
    return $distance;
  }
  
  $hav_distance = distance_haversine($lat_1, $lon_1, $lat_2, $lon_2);
  ?&gt;
  </code></pre>