How can PHP developers effectively integrate distance calculations in SQL queries for location-based filtering in their applications?

To effectively integrate distance calculations in SQL queries for location-based filtering in PHP applications, developers can use the Haversine formula to calculate distances between two sets of latitude and longitude coordinates. This formula can be implemented directly in the SQL query to filter results based on proximity to a specific location.

// User's latitude and longitude
$userLat = 37.7749;
$userLong = -122.4194;

// SQL query to select locations within a certain radius from the user's location
$sql = "SELECT *, 
        ( 3959 * acos( cos( radians($userLat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($userLong) ) + sin( radians($userLat) ) * sin( radians( lat ) ) ) ) AS distance
        FROM locations
        HAVING distance < 10
        ORDER BY distance";