0

我知道这个问题之前可能会被问到,但我想展示我的代码,我想知道是在做简单的事情还是让代码复杂化?此外,答案说使用trycatch但正在使用程序方法,所以我想知道我是否正确?

我做什么,例如联系表格

<?php
if(isset($_POST['contact'])) {
    $throw_error = array();

    //First Block Is Validation
    if($_POST['first_name'] == '' || $_POST['last_name'] == '') {
        $throw_error['field_blank'] = 'Fields Cannot Be Blank';
    } elseif(strlen($_POST['first_name']) < 3) {
         $throw_error['char_len'] = 'First Name Cannot Be Less Than 3 Characters';
    }

    //Second Block Is Process If No Errors Are Found
    if(empty($throw_error)) {
        $first_name = $_POST['first_name']; 
       //Don't worry about the sanitizing part, am doing it
       //Process the form ahead and then redirect using header()
       }
    } elseif(!empty($throw_error)) { //Third Block To Throw Error If Any Errors Found
        if(isset($throw_error['field_blank'])) {
            echo  $throw_error['field_blank'];
        } elseif(isset($throw_error['char_len'])) {
            echo  $throw_error['char_len'];
        }
    }
?>

<form>
   <input type="text" name="first_name" />
   <input type="text" name="last_name" />
   <input type="submit" value="Submit" name="contact" />
</form>
4

2 回答 2

1

I would also recommend using Try/Catch. You can still use it in procedural blocks.

If you are dead set against it, this code looks like it will do what you want it to do, but it's over-engineered. Since the user can either have a field_blank or char_len error, you only need a single string error variable.

$error = NULL;
if($_POST['first_name'] == '' || $_POST['last_name'] == '') {
    $error = 'Fields Cannot Be Blank';
} elseif(strlen($_POST['first_name']) < 3) {
     $error = 'First Name Cannot Be Less Than 3 Characters';
}

...

if(!empty($error)) {
   echo $error;
}

If you need to catch and display multiple non-exclusive errors, you can use the array you cited in your sample code and just iterate over the values rather than making a giant if/elseif block.

} elseif(!empty($throw_error)) { //Third Block To Throw Error If Any Errors Found
    foreach($throw_error as $key => $message) {
        echo $message.'<br>';
    }
}
于 2013-01-05T16:58:39.170 回答
1

尝试/抓住你在这里很棒。它消除了数组所需的一切throw_error,并大大减少了代码。

try{
    $first_name = '';
    if(isset($_POST['contact'])) {
        if($_POST['first_name'] == '' || $_POST['last_name'] == '') {
            throw new Exception('Fields Cannot Be Blank');
        } elseif(strlen($_POST['first_name']) < 3) {
             throw new Exception('First Name Cannot Be Less Than 3 Characters');
        }
        $first_name = $_POST['first_name'];
    }
}catch(Exception $e){
    echo $e->getMessage();
}

如果您希望将数组传递给这个 catch 语句,我发现的最简单的方法是传递serialize给数组,然后unserialize一旦完成。这将允许您一次打印所有错误:

try{
    $first_name = '';
    if(isset($_POST['contact'])) {
        if($_POST['first_name'] == '' || $_POST['last_name'] == '') {
            $throw_error[] = 'Fields Cannot Be Blank';
        }
        if(strlen($_POST['first_name']) < 3) {
             $throw_error[] = 'First Name Cannot Be Less Than 3 Characters';
        }
        if(isset($throw_error)){
            throw new Exception(serialize($throw_error));
        }
        $first_name = $_POST['first_name'];
    }
}catch(Exception $e){
    $errors = unserialize($e->getMessage());
    foreach($errors as $error){
        echo $error.'<br>';
    }
}
于 2013-01-05T16:43:07.250 回答