在PHP多表单验证中使用If语句的困难


Difficulties in working with If statement in PHP multiple form validation

我一直在尝试用PHP验证HTML表单,当然已经成功了。这个问题是,在进行所有验证后,我在哪里添加包含成功消息的包含文件时遇到了一些问题。这是我的代码:

<?php
        $status=$codeErr="";
        if (isset($_GET['check'])) {
            $number=$_GET['number'];
            $code=$_GET['code'];
            if($number!=""){
                if(preg_match("/[0-9]/",$number) and strlen($number)=="11"){
                    $four=substr($number, 0,4);
                    if($four!="0706" and $four!="0813" and $four!="0803" and $four!="0806" and $four!="0703" and $four!="0816" and $four!="0810" and $four!="0814" and $four!="0903"){
                    $status='<p class="status">Number Entered is not an MTN Number</p>';
                    }
                }
                else{
                    $status='<p class="status">Invalid Number Format</p>';
                }
            }
            else{
                    $status='<p class="status">Please Enter an MTN Number</p>';
                }
            if($code!=""){
                if($code!=="324"){
                    $codeErr='<p class="status">Winning Code Not Recognized</p>';
                }
            }
            else{
                $codeErr='<p class="status">Please Enter Your Winning Code</p>';
            }

        }

如果未设置,此包含文件将显示表单。

        else if(!isset($_GET['check'])){
            include('includes/check.php');
        }
    ?>

现在,我有了另一个包含文件,其中包含一条消息,如果在检查表单关闭时满足所有条件,将显示该消息。我该将其包含在哪里?

您应该使用异常:

try {
    if (!$somethingOne) {
        throw new Exception('Something wrong one!');
    }
    if (!$somethingTwo) {
        throw new Exception('Something wrong two!');
    }
    if (!$somethingMore) {
        throw new Exception('Something wrong more!');
    }
    // success here only
    echo 'All right!';
} catch (Exception $e) {
    echo $e->getCode() . '<p>' . $e->getMessage() . '</p>';
}