Home /PHP /Get an Australian State from a Postcode lookup /

Get an Australian State from a Postcode lookup

2:17 am, April 23, 2017

Here is a fairly (not exactly) accurate php function to get the australian state string from a postcode number range.

 

Usage:


echo get_state_au(2000); /* display the state code */
$state = get_state_au(2000); /* assign the state code to the variable $state. */

Code:


function get_state_au($postcode) {
$state = false;
if(!is_numeric($postcode)) { return false; }

if(($postcode >= 2000 && $postcode <= 2899)) { $state = "NSW"; } // nsw should be checked 1st as act may override it
if(($postcode >= 2600 && $postcode <= 2618)) { $state = "ACT"; }
if(($postcode >= 2900 && $postcode <= 2999)) { $state = "ACT"; }
if(($postcode >= 0800 && $postcode <= 0900)) { $state = "NT"; }
if(($postcode >= 4000 && $postcode <= 4999)) { $state = "QLD"; }
if(($postcode >= 5000 && $postcode <= 5999)) { $state = "SA"; }
if(($postcode >= 7000 && $postcode <= 7999)) { $state = "TAS"; }
if(($postcode >= 3000 && $postcode <= 3999)) { $state = "VIC"; }
if(($postcode >= 6000 && $postcode <= 6999)) { $state = "WA"; }

return $state;
}
function get_state_au($postcode) { $state = false; if(!is_numeric($postcode)) { return false; } if(($postcode >= 2000 && $postcode <= 2899)) { $state = "NSW"; } // nsw should be checked 1st as act may override it if(($postcode >= 2600 && $postcode <= 2618)) { $state = "ACT"; } if(($postcode >= 2900 && $postcode <= 2999)) { $state = "ACT"; } if(($postcode >= 0800 && $postcode <= 0900)) { $state = "NT"; } if(($postcode >= 4000 && $postcode <= 4999)) { $state = "QLD"; } if(($postcode >= 5000 && $postcode <= 5999)) { $state = "SA"; } if(($postcode >= 7000 && $postcode <= 7999)) { $state = "TAS"; } if(($postcode >= 3000 && $postcode <= 3999)) { $state = "VIC"; } if(($postcode >= 6000 && $postcode <= 6999)) { $state = "WA"; } return $state; }