0

I am in the making of some code that needs to check if a users login details are correct, and I therefore need a lot of if-statements inside each other. If any of the conditions in the if-statements are not true, they should alle return the same value. Is there an easy way of doing this, instead of writing the same multiple times? I have made an example below to visualize my problem. As you can see here I write " else { return false; }" multiple time, and this is what I am wondering if you are able to do more efficiently. Maybe so I only have to write "or else return false" once.

//some code
if (/*some condition*/) {
  //some code
  if (/*some new condition*/) {
    //some code
    if (/*some new condition*/) {
        //some code
      } else {
        return false;
      }
   } else {
     return false;
   }
} else {
  return false;
}

I am having a hard time finding a good way to explain my problem, so if you have a more elegant way of explaining it, do not hesitate to edit my post. I am also not quite sure that the title is as good as it could be, so if you have any ideas to an alternativ please say so :)

2 Answers 2

2

Lets say you have something like that (I added No):

if ( condition1 ) {
  //some code 1
  if ( condition2 ) {
    //some code 2
    if ( condition3 ) {
        //some code 3
      } else {
        return false;
      }
   } else {
     return false;
   }
} else {
  return false;
}

Since each time a condition is false, you exit the function returning false, you can directly test if the condition is false using a negation (if the negated condition is true):

if ( !condition1 ) {
    return false;
}
//some code 1
if ( !condition2 ) {
    return false;
}
//some code 2
if ( !condition3 ) {
    return false;
}
//some code 3

This doesn't reduce the number of if statements, but you avoid many nesting levels and the else statements.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah, okay, you got a point. Let us say I wanted to return the same string every time. How would that look?
@Chris: I'm tempted to say exactly the same, because returning a boolean, a string or whatever you want doesn't change anything. Obviously, if it is a long string, put it in a variable and return the variable. But perhaps a more realistic example of code will be helpful to better illustrate what you are really looking for.
Ah I understand now. I was concerned that the code would not stop after returning a string as it would when returning a boolean. But I guess that every time you return something, the code stops. Your answer is defiantly a good solution, but I think I am waiting a little with accepting it, hoping someone has a better solution :)
0

You can also try the switch statement. For many situations it will produce cleaner code.

<?php
if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}
?>

The switch statement is also compatible with using strings:

<?php
switch ($i) {
    case "apple":
        echo "i is apple";
        break;
    case "bar":
        echo "i is bar";
        break;
    case "cake":
        echo "i is cake";
        break;
}
?>

Good luck! :)

1 Comment

Hmm... I am not quite sure this is what I am looking for, but thank you for your answer anyway :))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.