Sosumi
Sosumi

Reputation: 779

What is the most logical structure for many if statements?

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?

Upvotes: 1

Views: 158

Answers (3)

Amal Murali
Amal Murali

Reputation: 76636

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

Upvotes: 1

Timbo_KZ
Timbo_KZ

Reputation: 88

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

Upvotes: 3

Jarek.D
Jarek.D

Reputation: 1294

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)

Upvotes: 0

Related Questions