1

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?

2
  • 3
    Unrelated to the actual question, but still important: Never use die as an actual error message to a user, there are always better ways to display that they've filled out a form wrong.
    – John V.
    Jul 20, 2013 at 15:15
  • Yeah, it's just a quick example to illustrate my question. I hope people don't try to learn PHP from my Stack Overflow examples...
    – Sosumi
    Jul 20, 2013 at 15:19

3 Answers 3

3

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...

1
  • 2
    Personally, I'd prefer to see all errors at once. It's kind of annoying to fix one error, then get another error. It's too much like debugging.
    – Herbert
    Jul 20, 2013 at 15:25
1

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 :(";
}
0

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)
2
  • 3
    That works; but what is an action needs to be performed if "$condition3" is met, but "$condition4" is not? I suppose this method loses that functionality.
    – Sosumi
    Jul 20, 2013 at 15:23
  • In that case sequential method is the only way. Or more object oriented approach - making condition a class ect
    – Jarek.D
    Jul 20, 2013 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.