PHP 3选项IF语句

时间:2015-01-05 14:13:22

标签: php error-handling

我目前正在学习PHP但遇到问题。目前我正在建立一个邮政编码检查员,检查我们(我们的公司)是否支持该地区的地方议会。问题是错误消息,我有一个“成功”和“错误”消息,但我还想显示一条错误消息,显示“抱歉,我们不支持您的理事会”。任何帮助将不胜感激!

PHP

if (isset($_POST['submitlogin']) && empty($_POST["postcode"])) {

echo '<div class="redalert">'."Please enter a Postcode".'</div>';
echo '<style>' .".enter {display:none;}". '</style>';

}

else {

if (isset($_POST['submitlogin'])) {
    echo '<style>' .".enter {display:none;}". '</style>';

        $postcodes = array("SW16","BN2","BN3","BN1","BN42");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">' . $brighton . '</div>';
        }
        $postcodes = array("PO18","RG14","GU29","PO20","GU28","PO19");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">' . $chichester .'</div>';
        }
        $postcodes = array("BN22","G42","G31","BN23","BN21","BN20","BN24");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">' . $eastbourne . '</div>';
        }
        $postcodes = array("SO32","SO50","SO30","SO31","SO53","SO16","SO21","SO52");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">' . $eastleigh . '</div>';
        }
        $postcodes = array("KT12","KT14","KT10","KT11","KT8","KT6","KT13","KT22","KT7");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">'.$elmbridge.'</div>';
        }
        $postcodes = array("PO12","PO13","PO14");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">'.$gosport.'</div>';
        }
        $postcodes = array("GU15","GU3","GU7","KT11","GU2","GU5","GU1","GU24","TW20","GU3","GU23","GU10","GU7","GU4","GU5","GU2","GU10","GU7","GU21","GU3","GU4","GU3","GU1","GU5","GU3","GU12","GU1","GU7","GU4","GU8","GU23","GU4","GU7","GU4","GU3","KT24","GU23","GU3","GU7","GU5","GU23","GU5","RH5","GU4","KT24","GU7","GU8","GU1");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">'.$guildford.'</div>';
        }
        $postcodes = array("GU17","GU52","GU10","GU51","RG27","GU10","GU51","RG29","GU46");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">'.$hart.'</div>';
        }
        $postcodes = array("TN34","TN33","TN38","TN35","TN37");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">'.$hastings.'</div>';
        }
        $postcodes = array("BN8","BN6","BN7","BN9","BN10","BN25","RH17");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">'.$lewes.'</div>';
        }
        $postcodes = array("KT15","KT16","TW20","TW18","KT15","GU25");
        if (in_array($input, $postcodes)) {
            echo  '<div class="alert">'.$runnymede.'</div>';
        }
        $postcodes = array("GU11","GU17","GU14","GU9");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">'.$rushmoor.'</div>';
        }
        $postcodes = array("BN26","TN33","TN22","BN8","TN6","RH17","RH18","BN27","TN21","BN24","TN5","TN7");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">'.$wealden.'</div>';
        }
        $postcodes = array("KT14","GU24","GU21","GU22","GU4");
        if (in_array($input, $postcodes)) {
            echo '<div class="alert">'.$woking.'</div>';
        }




    }

 }
    ?>

我尝试在每个之后添加一个Else,但没有取得任何成功。任何指导都会很棒。

亲切的问候

2 个答案:

答案 0 :(得分:0)

如果这些if()语句中的任何一个发现匹配,您只需要跟踪:

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

if (!$found) {
   die("Council not supported");
}

答案 1 :(得分:0)

您可以使用elseif语句执行此操作:

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 / elseif / else块仅包含1行,因此您可以将大括号括起来:

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>';

请注意,当您使用大括号时,可以使用else ifelseif - 但是当您使用else if(中间有空格)将大括号括起来时会失败解析错误。