PHP3选项IF语句

PHP3选项IF语句,php,error-handling,Php,Error Handling,我目前正在学习PHP,但遇到了一些问题。目前,我正在建立一个邮政编码检查器,检查我们(我们的公司)是否支持该地区的地方议会。问题是错误消息,我有一条“成功”和一条“错误”消息,但我也希望看到一条显示“对不起,我们不支持你的委员会”的错误消息。任何帮助都将不胜感激 PHP if(设置($\u POST['submitlogin'])和空($\u POST[“postcode”])){ 回显“”。“请输入邮政编码”。”; 回显“”。。输入{display:none;}“; } 否则{ 如果(isse

我目前正在学习PHP,但遇到了一些问题。目前,我正在建立一个邮政编码检查器,检查我们(我们的公司)是否支持该地区的地方议会。问题是错误消息,我有一条“成功”和一条“错误”消息,但我也希望看到一条显示“对不起,我们不支持你的委员会”的错误消息。任何帮助都将不胜感激

PHP

if(设置($\u POST['submitlogin'])和空($\u POST[“postcode”])){
回显“”。“请输入邮政编码”。”;
回显“”。。输入{display:none;}“;
}
否则{
如果(isset($_POST['submitlogin'])){
回显“”。。输入{display:none;}“;
$postcodes=数组(“SW16”、“BN2”、“BN3”、“BN1”、“BN42”);
if(在_数组中($input,$postcodes)){
回声“.$brighton.”;
}
$postcodes=数组(“PO18”、“RG14”、“GU29”、“PO20”、“GU28”、“PO19”);
if(在_数组中($input,$postcodes)){
回声“.$chichester.”;
}
$postcodes=数组(“BN22”、“G42”、“G31”、“BN23”、“BN21”、“BN20”、“BN24”);
if(在_数组中($input,$postcodes)){
echo“.$eastbourne.”;
}
$postcodes=数组(“SO32”、“SO50”、“SO30”、“SO31”、“SO53”、“SO16”、“SO21”、“SO52”);
if(在_数组中($input,$postcodes)){
回声“.$eastleigh.”;
}
$postcodes=数组(“KT12”、“KT14”、“KT10”、“KT11”、“KT8”、“KT6”、“KT13”、“KT22”、“KT7”);
if(在_数组中($input,$postcodes)){
回声“.$elmbridge.”;
}
$postcodes=数组(“PO12”、“PO13”、“PO14”);
if(在_数组中($input,$postcodes)){
回显“.$gosport.”;
}
$postcodes=数组(“GU15”、“GU3”、“GU7”、“KT11”、“GU2”、“GU5”、“GU1”、“GU24”、“TW20”、“GU3”、“GU23”、“GU10”、“GU4”、“GU5”、“GU2”、“GU10”、“GU7”、“GU21”、“GU3”、“GU4”、“GU3”、“GU1”、“GU5”、“GU3”、“GU12”、“GU1”、“GU7”、“GU4”、“GU8”、“GU23”、“GU7”、“GU4”、“GU3”、“GU5”、“GU23”、“GU5”、“GU5”、“GU4”、“GU24”、“GU8”、“GU1”);
if(在_数组中($input,$postcodes)){
回音“.$guildford.”;
}
$postcodes=数组(“GU17”、“GU52”、“GU10”、“GU51”、“RG27”、“GU10”、“GU51”、“RG29”、“GU46”);
if(在_数组中($input,$postcodes)){
回声“.$hart.”;
}
$postcodes=数组(“TN34”、“TN33”、“TN38”、“TN35”、“TN37”);
if(在_数组中($input,$postcodes)){
回声“.$hastings.”;
}
$postcodes=数组(“BN8”、“BN6”、“BN7”、“BN9”、“BN10”、“BN25”、“RH17”);
if(在_数组中($input,$postcodes)){
回声“.$lewes.”;
}
$postcodes=数组(“KT15”、“KT16”、“TW20”、“TW18”、“KT15”、“GU25”);
if(在_数组中($input,$postcodes)){
回声“.$runnymede.”;
}
$postcodes=数组(“GU11”、“GU17”、“GU14”、“GU9”);
if(在_数组中($input,$postcodes)){
回声“.$rushmoor.”;
}
$postcodes=数组(“BN26”、“TN33”、“TN22”、“BN8”、“TN6”、“RH17”、“RH18”、“BN27”、“TN21”、“BN24”、“TN5”、“TN7”);
if(在_数组中($input,$postcodes)){
回声“.$wealden.”;
}
$postcodes=数组(“KT14”、“GU24”、“GU21”、“GU22”、“GU4”);
if(在_数组中($input,$postcodes)){
回声“.$woking.”;
}
}
}
?>
我尝试在每一个之后添加一个Else,但没有成功。任何指导都很好


您只需跟踪那些
if()
语句中的任何语句是否找到匹配项:

$found = false;
if(in_array(...)) {
   $found = true;
}
if(in_array(...)) {
   $found = true;
}
... lots more ifs() ...

if (!$found) {
   die("Council not supported");
}
您可以通过以下语句执行此操作:

if (in_array($input, $postcodes = array("SW16","BN2","BN3","BN1","BN42"))) {
    echo '<div class="alert">' . $brighton . '</div>';
} elseif (in_array($input, $postcodes = array("PO18","RG14","GU29","PO20","GU28","PO19"))) {
    echo '<div class="alert">' . $chichester .'</div>';
} elseif (in_array($input, $postcodes = array("BN22","G42","G31","BN23","BN21","BN20","BN24"))) {
    echo '<div class="alert">' . $eastbourne . '</div>';
} //a lot more else ifs
else { //none of the above if conditions were met
    echo '<div class="alert">Invalid postal code!</div>';
}

请注意,当您使用花括号时,您可以使用
else if
elseif
——但是当您使用
else if
(中间有空格)将花括号去掉时,将出现解析错误。

我将尝试一下,谢谢!谢谢你的时间,我现在工作得很好
if (in_array($input, $postcodes = array("SW16","BN2","BN3","BN1","BN42"))) {
    echo '<div class="alert">' . $brighton . '</div>';
} elseif (in_array($input, $postcodes = array("PO18","RG14","GU29","PO20","GU28","PO19"))) {
    echo '<div class="alert">' . $chichester .'</div>';
} elseif (in_array($input, $postcodes = array("BN22","G42","G31","BN23","BN21","BN20","BN24"))) {
    echo '<div class="alert">' . $eastbourne . '</div>';
} //a lot more else ifs
else { //none of the above if conditions were met
    echo '<div class="alert">Invalid postal code!</div>';
}
if (in_array($input, $postcodes = array("SW16","BN2","BN3","BN1","BN42")))
    echo '<div class="alert">' . $brighton . '</div>';
elseif (in_array($input, $postcodes = array("PO18","RG14","GU29","PO20","GU28","PO19")))
    echo '<div class="alert">' . $chichester .'</div>';
elseif (in_array($input, $postcodes = array("BN22","G42","G31","BN23","BN21","BN20","BN24")))
    echo '<div class="alert">' . $eastbourne . '</div>';
else
    echo '<div class="alert">Invalid postal code!</div>';