How to search by nearest UK postcode in PHP

Often clients ask me to create a function where by a visitor can come onto their website, there they can enter in their postcode, the idea is to then display the closest location based on locations in a database.

Aim: To display details of a predefined location that is closest to the visitor based on the postcode they enter.

To achieve this you must cross reference their postcode and your locations with a numerical value (such as coordinates).

This means we require a database of postcodes and such numerical values.

This gives us a few choices:

  • Royal Mail’s PAF – This is very costly, I’d not recommend this.
  •’s – Not been updated since 2004, may no longer be online.
  •’s Postcode database – Also has lots of information on how to use such a database
  •’s Database – Kinda crappy site, with limited info, but has a database.
  • New Popular Edition Maps – A very popular postcode database
  • ’s database – Fairly new, notable.
  • Google Maps UK – If all else fails a bit of API magic and this may just do the trick. (Also see: Map Maker)

For some of you Google Maps might be the most appropriate option, however for me, my client is particularly fussy and only wants to display the details of the closest location, not the details of all the locations. Using Google Maps would require plotting all the locations on the map before you even enter the postcode.

Having said all this, a quick browse through the “External Links” on the UK Postcodes Wikipedia entry, and I quickly found an article by Paul Jenkins entitled UK Post Code Distance Calculation in PHP, which is fantastic, you can even download it here (

After a short examination it seems this does exactly what it says on the tin, and simply calculates the distance.

However, with a quick google for php distance calculation, you can quickly find that there are more refined equivalents of the distance calculation. I thought it might be a good idea to use one of those instead.

After a bit of tweaking, here’s what I came up with in the end:

function distance($lat1, $lon1, $lat2, $lon2, $u=’1′) {

if ($u == ‘k’) { $u=1.609344; } // kilometers
elseif ($u == ‘n’) { $u=0.8684; } // nautical miles
elseif ($u == ‘m’) { $u=1; } // statute miles (default)


$d=($d*$u); // apply unit
$d=round($d); // optional
return $d;

So, that’s the hard parts done (database and maths), next is simply a case of using this information to “find the closest” from the postcode we input to an array of postcodes we supply…

To find the “closest” postcode, effectively what we’re trying to do is find the “shortest” distance between the postcodes, or, simply the smallest number in the results, assuming we put the results into an array with the key as the postcode and the distance as the value.

All we have to do is create a simple script that will find the smallest number in a given array, then return the appropriate key. Simple!

function closest ($needle,$haystack) {
if (!$needle || !$haystack) { return; }
if (!is_array($haystack)) { return; }

$smallest=min($haystack); //smallest value

foreach ($haystack as $key => $val) {
if ($val == $smallest) { return $key; }

The above script does exactly what we want, using the “min” function we can quickly work out what we need to return.

The only task left is to bind all this together, we need to create two functions that will:

  1. Get the distance using the postcode to get the longitude and latitude from the database.
  2. Create an array with the postcodes as the keys, and the distance as the values.

Very simple!

Function 1, Postcode Distance

function postcode_distance ($from,$to) {
// Settings for if you have a different database structure

// This is a check to ensure we have a database connection
if (!@mysql_query(‘SELECT 0′)) { return; }

// Simple regex to grab the first part of the postcode

$sql = “SELECT `$lat`, `$lon` FROM `$table` WHERE `$postcode`=’$one’”;
$query = mysql_query($sql);
$one = mysql_fetch_row($query);

$sql = “SELECT `$lat`, `$lon` FROM `$table` WHERE `$postcode`=’$two’”;
$query = mysql_query($sql);
$two = mysql_fetch_row($query);

$distance = distance($one[0], $one[1], $two[0], $two[1]);

// For debug only…
//echo “The distance between postcode: $from and postcode: $to is $distance miles\n”;

return $distance;

Function 2, Postcode Closest

function postcode_closest ($needle,$haystack) {
if (!$needle || !$haystack) { return; }
if (!is_array($haystack)) { return; }

foreach ($haystack as $postcode) {
return closest($needle,$results);

So, with that done, place the 4 above functions into a file such as “postcode.php”, ready for use in the real world…

Test case:



if ($_POST) {
$postcodes=array(‘TF9 9BA’,'ST4 3NP’);

if (isset($closest)) {
echo “The closest postcode is: $closest”;

<form action=”" method=”post”>
Postcode: <input name=”postcode” maxlength=”9″ /><br />
<input type=”submit” />

You can download this script here: postcode_search.phps

Note: In the above test case, I have a “db.php” file which contains my database details and starts a database connection. I suggest you do the same.

Ensure you have your database populated, you should be able to use Paul Jenkins’s UK Postcode csv, allowing you to use your own table structure.

Well, that’s all folks, I can now use this script to provide any locations that match the “closest” postcode.


Related posts:

  1. Does registering a domain for a longer term increase your search engine rankings? Recently I come across a claim by Network Solutions stating:...


  1. Leon Chevalier said,

    December 17, 2007 @ 9:47 am


    Thought I’d let you know that Google now allows Geocoding of UK postcodes via their API. This means that you can quite easily build nearest store finder without a database of UK postcodes. If you’ll allow the plug, I have written a tutorial on the whole thing over at my site: Perhaps you would consider including it in your list of choices?



  2. Gary Hides said,

    January 8, 2008 @ 5:42 pm

    Nice work people!

  3. the site company said,

    May 19, 2008 @ 8:19 am

    nice to see some actual ways to implement this, i have tried working with google maps, but getting something which is automated is a real pain.Thanks to one of the links above i have a decent postcode list to work with.

  4. Dan said,

    August 16, 2008 @ 4:08 pm

    Thanks for this but are you you able to provide this as a download aas my php is very limited?

    Thanks in advance.

  5. Andrew said,

    January 20, 2009 @ 10:21 am

    Thank you for this write up, it has been extremely useful!

  6. Karl said,

    March 3, 2009 @ 5:38 pm

    This looks great, exactly what i am after!

    I am a newbie to PHP so was wondering if you could help me?

    I have created my Database and was wondering how i import the CSV you specified into the table? For some reason when i import it, nothing happens.

    One more thing, how would i make the search open a new page once the query has been processed?

  7. PHP Postcode - DesignersTalk said,

    March 4, 2009 @ 12:31 pm

    [...] know the basics. I managed to find hopefully something similar to what they are after on this site: � How to search by nearest UK postcode in PHP but am not sure how to implement. Can anyone shed any light on how to go about this? I have tried [...]

  8. Andy said,

    June 17, 2009 @ 11:13 pm


    It’s about 6 months old, so still pretty much up to date.

    It has 2.3 million rows, and is 72 MB uncompressed.

    If anyone would like a copy, the price is 80 pounds. My email is

  9. Ian said,

    October 23, 2009 @ 4:23 pm

    What an increadibly useful article. I can’t thank you enough for this! I was about to tell my client that they would have to look elsewhere but now I can not only provide the functionality, but ask them what list they want to use (and tell them that they dont have to spend £££’s buying the post office’s list)!

    I will ensure that any info that I find during ‘code tweaking’ will be posted here.

    Once again, thanks for such a fantastic tutorial.

RSS feed for comments on this post · TrackBack URL

Leave a Comment