以零開頭的郵政編碼第一位數下面的PHP代碼應返回的$zone
5.與$postcodeprefix
應該是075.如何獲得PHP
我相信,因爲PHP是治療的郵政編碼這是不工作作爲整數而不是字符串。我試過這樣的東西:
$postcode = $postcode." ";
$postcode = strval($postcode);
我嘗試過的東西都沒有工作。
什麼是修復?
$postcode = 07558;//comes from database as a number. I can't change this.
$postcode = $postcode." "; //one of various ways I have tried to turn this into a string
$postcode = trim($postcode);
$zone = 99;
$postcodeprefix = substr($postcode,0,3);
echo "\$postcodeprefix=".$postcodeprefix."\n";
$postcodeprefixkey = substr($postcode,0,1); //this is the first postcode digit
echo "\$postcodeprefixkey=".$postcodeprefixkey."\n";
if ($postcodeprefixkey == 0) {
//any range containing postcode which starts with 0
if ($postcodeprefix >= 001 && $postcodeprefix <= 005) {$zone = 5;} else
if ($postcodeprefix >= 006 && $postcodeprefix <= 009) {$zone = 6;} else
if ($postcodeprefix >= 010 && $postcodeprefix <= 029) {$zone = 5;} else
if ($postcodeprefix >= 030 && $postcodeprefix <= 054) {$zone = 6;} else
if ($postcodeprefix >= 055 && $postcodeprefix <= 055) {$zone = 5;} else
if ($postcodeprefix >= 056 && $postcodeprefix <= 059) {$zone = 6;} else
if ($postcodeprefix >= 060 && $postcodeprefix <= 098) {$zone = 5;}
}
echo "\$zone=".$zone;
$ postcode =(string)$ postcode; – Slime
@slime - 那不行。我嘗試過這個。 – MountainX
郵政編碼是字符串,而不是數字。 – Brad