以下PHP代码应返回$zone
为5. $postcodeprefix
应为075.
我认为这不起作用,因为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;
答案 0 :(得分:0)
问题出在这一行:
$postcode = 07558;//comes from database as a number. I can't change this.
在这里,$ 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);
echo $postcode;
给出493。
将其视为一个数字,这很容易:
$postcode = 7558; // 07558 would be treated as octal value!
$postcodeC = intval($postcode / 100); // 075
if ( $postcodeC < 100 )
{
...
if ($postcodeC >= 60 && $postcodeC <= 98) {$zone = 5;}
...
}
答案 1 :(得分:0)
以下是我最终解决的问题。我欺骗了substr()函数,没有使用这种技术将$ postcode重新解释为八进制数:
$postcodeprefix = substr("x".$postcode,1,3);
$postcodeprefixkey = substr("x".$postcode,1,1);