hellomello
hellomello

Reputation: 8587

php comparing two numbers together

Does PHP have some kind of function that will compare the numbers? I know they have text comparison like similar_text() But say I want to compare lat/lng values, and they're very similar but very slight difference

example :

place 1 

lat: 34.095161
lng: -117.720638

compare with

lat: 34.0948841
lng: -117.7206854

place 2

lat: 34.094572563643
lng: -117.72184828904

compare with 

lat: 34.094112
lng: -117.7250746

They maybe very close to each other which I want to be able to get a certain range? Is there some PHP function I can use?

Thanks

Upvotes: 2

Views: 2918

Answers (2)

AsTeR
AsTeR

Reputation: 7521

I don't think there's a built in function. Maybe you could use an Euclidian distance to compute their similarity is an easy-to-implement way.

$diff = sqrt((($lat1 - $lat2) * ($lat1 - $lat2)) + (($lng1 - $lng2) * ($lng1 - $lng2)));
$epsilon = 0.0001;
if ($diff < $espilon) {
    echo 'we consider them equal';
} else {
    echo 'they are different';
}

NB : $diff is here a pure quantification of difference, you cannot consider lat/lng values as forming 2D points whose distance can be compute through Euclidian approach stricto sensu (see here why)

Upvotes: 3

Elzo Valugi
Elzo Valugi

Reputation: 27866

Also take a look at Haversine formula. Usually used to see how big is the distance between 2 points on a sphere.

Upvotes: 2

Related Questions