title: PHP returning NaN
body: <p>I have a function that calculates the distance between two GPS coordinates. I then get all the coordinates from the database and loop through them all to get the distance between the current one and the previous one, then add that to an array for the specific GPS device. For some reason it is return NaN. I have tried casting it as a double, an int, and rounding the number.</p>
  
  <p>Here is my PHP code:</p>
  
  <pre><code>function distance($lat1, $lon1, $lat2, $lon2) {
        $lat1 = round($lat1, 3);
        $lon1 = round($lon1, 3);
        $lat2 = round($lat2, 3);
        $lon2 = round($lon2, 3);
        $theta = $lon1 - $lon2; 
        $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
        $dist = acos($dist); 
        $dist = rad2deg($dist); 
        $miles = $dist * 60 * 1.1515;
        if($miles &lt; 0) $miles = $miles * -1;
        return ($miles * 1.609344);  
  }
  $this-&gt;db-&gt;query("SELECT * FROM `gps_loc` WHERE `imeiN`='" . $sql . "' AND `updatetime`&gt;=$timeLimit ORDER BY `_id` DESC");
      $dist = array();
      $dist2 = array();
      while($row = $this-&gt;db-&gt;getResults()) {
          $dist2[$row['imeiN']] = 0;
          $dist[$row['imeiN']][]["lat"] = $row['lat'];
          $dist[$row['imeiN']][count($dist[$row['imeiN']]) - 1]["lng"] = $row['lon'];
      }
  
      foreach($dist as $key=&gt;$d) {
          $a = 0;
          $b = 0;
          foreach($dist[$key] as $n) {
              if($a &gt; 0) {
                  $dist2[$key] += $this-&gt;distance($n['lat'], $n['lng'], $dist[$key][$a - 1]['lat'], $dist[$key][$a - 1]['lng']);
              }
              $a++;
          }
  
      }
      echo json_encode($dist2);
  </code></pre>