嗨,在检查正确的"postnummer"后,我得到了一个查询的语句来发布特定的数据。
if($_POST['postnummer'] == "7900" or "7950" or "7960") {
$region = "Nordjylland";
}
elseif ($_POST['postnummer'] == "8654" or "8660" or "8680" or "8700") {
$region = "Midtjylland";
}
但每次发布的值都是"Nordjylland"?
我认为应该使用数组
$nordjyllandRegions = array("7900","7950","7960");
$midtjyllandRegions = array("8654","8660","8680","8700");
$zipcode = $_POST['postnummer'];
if(in_array($zipcode, $nordjyllandRegions)) {
$region = "Nordjylland";
}
elseif (in_array($zipcode, $midtjyllandRegions)) {
$region = "Midtjylland";
}
您必须编写
if($_POST['postnummer'] == "7900" || $_POST['postnummer'] == "7950" || $_POST['postnummer'] == "7960") {
$region = "Nordjylland";
}
elseif ($_POST['postnummer'] == "8654" || $_POST['postnummer'] == "8660" || $_POST['postnummer'] == "8680" || $_POST['postnummer'] == "8700") {
$region = "Midtjylland";
}
您也可以使用switch语句,它更容易阅读:
switch ($_POST['postnummer']) {
case "7900":
case "7950":
case "7960":
$region = "Nordjylland";
break;
case "8654":
case "8660":
case "8680":
case "8700":
$region = "Midtjylland";
break;
default:
$region = "no match";
}
$postNummer = (int) $_POST['postnummer'];
if( in_array( $postNummer, array( 7900, 7950, 7960 ) ) )
{
$region = "Nordjylland";
}
elseif( in_array( $postNummer, array( 8654, 8660, 8680, 8700 ) ) )
{
$region = "Midtjylland";
}
或
$postNummer = (int) $_POST['postnummer'];
switch( $postNummer )
{
case 7900:
case 7950:
case 7960:
$region = "Nordjylland";
break;
case 8654:
case 8660:
case 8680:
case 8700:
$region = "Midtjylland";
break;
}