douxunchen3498
2013-07-20 15:12
浏览 67
已采纳

许多if语句的最合理的结构是什么?

I'm currently creating a simple login script in PHP, and there are many conditions that must be met for a successful login. To my knowledge, there are two ways of formatting the if statements that are required.

The first way, is using nested if statements:

if ($condition1 == true) {
    if ($condition2 == true) {
        if ($condition3 == true) {
            //Successful login
        } else {

        }
    } else {

    }
}

And the second way is using procedural if statements:

if ($condition1 != true) {
    die("error");
}
if ($condition2 != true) {
    die("error");
}
if ($condition3 != true) {
    die("error");
}

//successful login

Which of the two methods of dealing with many conditions is considered more logical? Are there any other methods that I haven't listed here which are better, and more logical?

图片转代码服务由CSDN问答提供 功能建议

我目前正在PHP中创建一个简单的登录脚本,并且成功必须满足许多条件 登录。 据我所知,有两种方法可以格式化所需的if语句。

第一种方法是使用嵌套的if语句:

 <  code> if($ condition1 == true){
 if($ condition2 == true){
 if($ condition3 == true){
 //登录成功
}否则{
 
} \  n} else {
 
} 
} 
   
 
 

第二种方法是使用过程if语句:

   if($ condition1!= true){
 die(“error”); 
} 
if($ condition2!= true){
 die(“error”); 
} 
if($  condition3!= true){
 die(“error”); 
} 
 
 //成功登录
   
 
 

处理这两种方法中的哪一种 有很多条件被认为更合乎逻辑? 我还没有列出哪些更好,更合理的方法吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanlinpi0265 2013-07-20 15:22
    已采纳

    In this case it doesn't really matter, unless you want to show the user all the errors you've encountered.

    For example, you can do this:

    if($condition1 != true) {
        if($condition2 != true) {
            if($condition3 != true) {
                // Success! Login now...
            } else {
                die(error3);
            }
        } else {
            die(error2);
        }
    } else {
        die(error1);
    }
    

    In this case user will be forced to fix errors as they appear. This might take more time but is easier to understand.

    if ($condition1 != true) {
        $error = 'Error 1';
    }
    if ($condition2 != true) {
        $error .= 'Error 2';
    }
    if ($condition3 != true) {
        $error .= 'Error 3';
    }
    
    if($error != '') {
        die($error);
    } else {
        // Success! Login now...
    }
    

    In this case, user will see all errors at the same time and he or she will be able to fix all errors before submitting the form again, but more information is always harder to understand.

    Update:

    John said the right thing.

    Somewhere below, near the login form and inputs you can make a php code like this:

    <?php if($error != '') { ?><p style="color:red;"><?php echo $error; ?></p><?php } ?>
    

    Obviously, you need to use the a code like the one I've shown above to prevent the server to attempt login if an error is found.

    Hope this helps...

    点赞 打赏 评论
  • douyi02577 2013-07-20 15:19

    That's more logical to me. As it represents a 'logic of login decision' in one statement and would be still the most readable if logic will get more messy down the line

    if (($condition1 == true) 
        && ($condition2 == true)
        && ($condition3 == true)
        && ($condition4 == true)
        && ($condition5 == true)
    
    点赞 打赏 评论
  • dounao2829 2013-07-20 15:26

    I'd do something like this:

    if(!(condition1)){
        $ok = false;
    }
    if(!(condition2)){
        $ok = false;
    }
    if(!(condition3)){
        $ok = false;
    }
    
    if($ok){
        //if they got here, they must have passed all the tests!
    }
    

    Can be shortened to:

    if( !(condition3) || !(condition2) || !(condition3)  ){
        echo "Success!";
    }
    

    From your comments:

    what if "$condition3" is met, but "$condition4" is not?

    You can use something like this:

    if( (condition1) && !(condition4) ) {
    echo "Hurray! Condition 1 was satisified but condition 4 failed :(";
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题