Introduction:
Finding the distance between two coordinate points is an essential task in many applications. This process becomes easier with the use of PHP, which offers inbuilt functions to calculate the distance between two coordinates.
In this article, we will discuss how you can find the distance between two coordinates in PHP. We will also provide you some code examples to understand the concept better.
Prerequisites:
To understand this article, you need to be familiar with:
 Basic knowledge of PHP.
 Mathematical concepts like Pythagoras theorem, latitude, and longitude.
Let's start with the basic concept first.
What is latitude and longitude?
Latitude and longitude are coordinates that specify a location's position on the Earth's surface. It is used to navigate and find the distance between two locations. Latitude is an angular measurement that specifies the NorthSouth position on the Earth's surface. It ranges from 0 degrees at the equator to 90 degrees at the poles. Longitude is an angular measurement that specifies the EastWest position on the Earth's surface. It ranges from 0 degrees at the Prime Meridian to 180 degrees at the International Date Line.
What is Pythagoras theorem?
Pythagoras theorem is a mathematical concept that states that the sum of the squares of the two legs of a rightangled triangle is equal to the square of the hypotenuse. It means that if you know the length of two sides of a rightangled triangle, you can calculate the length of the third side using this theorem.
Now, let's move on to the actual topic.
How to find the distance between two coordinates in PHP?
To find the distance between two coordinates in PHP, you can use the Haversine formula. The Haversine formula is a mathematical formula that calculates the distance between two points on the surface of a sphere. In our case, we will use this formula to calculate the distance between two points on the Earth's surface.
Here is the formula to calculate the distance between two coordinates using the Haversine formula:
formula
Where,
 R = the radius of the Earth.
 phi1 and phi2 = the latitude of point 1 and point 2 in radians.
 delta_phi and delta_lambda = the difference between the longitudes and latitudes of point 1 and point 2 in radians.
Note: The above formula is in radians. You need to convert the values to radians before using this formula.
Let's see how you can use this formula in PHP.
Code Example:
Here is the code example to calculate the distance between two coordinates using the Haversine formula in PHP:
“; // Output: 343.17463607234 Kilometers
echo distance(51.5074, 0.1278, 48.8566, 2.3522, “M”) . ” Miles
“; // Output: 213.38184296901 Miles
?>
Explanation:
In the above code example:
 The function distance() takes 5 arguments: the latitude and longitude of point 1, the latitude and longitude of point 2, and the unit in which you want to measure the distance.
 $theta = $lon1 – $lon2; calculates the difference between the longitudes of the two points.
 $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); calculates the Haversine formula's first part.
 $dist = acos($dist); calculates the arc cosine of the given value.
 $dist = rad2deg($dist); converts the given value from radians to degrees.
 $miles = $dist * 60 * 1.1515; multiplies the distance by the number of minutes per degree and the number of nautical miles per minute to get the distance in nautical miles.
 If the unit is 'K', you return the distance in kilometers, if the unit is 'N', you return the distance in nautical miles, else you return the distance in miles.
 Finally, you call the distance() function with the required parameters.
Conclusion:
In this article, we discussed how you can find the distance between two coordinates in PHP. We also provided you with a code example to understand the concept better. You can use this technique to calculate the distance between two coordinates in your applications.
here are some additional details about the topics we covered in the previous article.
Latitude and Longitude:
Latitude and longitude are commonly used to specify the location of a point on the Earth's surface. They are represented in degrees, minutes, and seconds, with positive values representing points north of the equator (for latitude) and east of the Prime Meridian (for longitude).
Latitude is the angle between the equator and the point, measured in degrees. The equator is assigned a latitude of 0 degrees, and the poles are assigned a latitude of 90 degrees north or south. The values of latitude range from 90 degrees to +90 degrees.
Longitude is the angle between the Prime Meridian and the point, measured in degrees. The Prime Meridian is assigned a longitude of 0 degrees, and the values of longitude range from 180 degrees to +180 degrees, with positive values indicating locations east of the Prime Meridian and negative values indicating locations west of it.
Haversine Formula:
The Haversine formula is a mathematical formula used to calculate the distance between two points on the surface of a sphere. It takes into account the curvature of the Earth's surface, making it particularly useful for calculations involving geographic coordinates.
To apply the Haversine formula, you need to know the latitude and longitude of the two points in question. Using these values, you can calculate the difference in latitude and longitude between the two points. These differences are then used to calculate the distance between the two points using the Haversine formula.
The formula takes into account the radius of the Earth, which is approximately 6,371 kilometers (3958.8 miles) or 6,378 kilometers (3963.2 miles), depending on whether you are using a mean or equatorial radius.
Conclusion:
In this article, we have discussed in more detail the concepts of latitude and longitude and the Haversine formula, which are essential for calculating the distance between two points on the Earth's surface. We hope this additional information will help you to better understand and apply these concepts in your own projects.
Popular questions

What is the Haversine formula?
Answer: The Haversine formula is a mathematical formula used to calculate the distance between two points on the surface of a sphere. It takes into account the curvature of the Earth's surface, making it useful for calculating the distance between two geographic coordinates. 
What is latitude and longitude?
Answer: Latitude and longitude are coordinates used to specify a location's position on the Earth's surface. Latitude is an angular measurement that specifies the NorthSouth position, while longitude is an angular measurement that specifies the EastWest position. 
How do you convert degrees to radians in PHP?
Answer: To convert degrees to radians in PHP, you can use the deg2rad() function. For example, to convert 90 degrees to radians, you would use the following code: deg2rad(90) 
What is the radius of the Earth?
Answer: The radius of the Earth is approximately 6,371 kilometers (3958.8 miles) or 6,378 kilometers (3963.2 miles), depending on whether you are using a mean or equatorial radius. 
What is the output of the code example in the article?
Answer: The output of the code example in the article is "343.17463607234 Kilometers" when the unit of measurement is kilometers and "213.38184296901 Miles" when the unit of measurement is miles. These values represent the distance between two geographic coordinates in London and Paris.
Tag
Geodesy