Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Nested IF statements [closed]

Tags:

php

nested-if

What control structures can one use instead of multiple nested IF statements.

eg:

function change_password($email, $password, $new_password, $confirm_new_password)
{
    if($email && $password && $new_password && $confirm_new_password)
    {
        if($new_password == $confirm_new_password)
        {
            if(login($email, $password))
            {
                if(set_password($email, $new_password))
                {
                    return TRUE;
                }
            }
        }
    }
}       

This function is used like this:

if(!change_password($email, $password, $new_password, $confirm_new_password)
{
    echo 'The form was not filled in correctly!';
    exit;
}

I call all my functions like this, and I'm wondering if there's something wrong with my coding style. I'm having my doubts because if I follow this design then that means every single function I write will just be with nested with IF's, checking if there are errors at every stage. Is this what other people do?

I don't see many other scripts written like this, with the nested IF's making a triangle shape and only having the desired result in the very middle. If the middle isn't reached, then something screwed up.

Is this a good function structure?

like image 674
Kausheel Avatar asked Aug 07 '12 04:08

Kausheel


People also ask

Can you nest if statements in PHP?

We can make nesting statement by nesting the if statement. It means when we insert a second if statement inside an if statement, we call it nested if statement or nesting the if statement.

How do you end an if statement in PHP?

The endif keyword is used to mark the end of an if conditional which was started with the if(...): syntax. It also applies to any variation of the if conditional, such as if... elseif and if...else .

What is difference between if and nested IF?

The nested if is an if statement used within another if statement. When we use if else if then an if statement is used within the else part of another if in this way,'nested if is similar to an if else if statement.


2 Answers

Nesting too deeply is generally a bad idea - it's spaghetti logic and difficult to follow. Since each of your verification steps depends on the previous stage having succeeded, don't nest at all - just bail out when a stage fails:

function change_password(blah blah blah) {
   if (!$condition1) {
      return false;
   }
   if (!$condition2) {
      return false;
   }
   etc....


   // got here, must have succeeded
   return true;
}

That makes it explicitly clear what the logic sequence is.

like image 115
Marc B Avatar answered Sep 23 '22 04:09

Marc B


I think it is definitely well readable and can easily be understood in comparison to using just one if statement like

if (blah and blah and blah and blah and blah and blah and blah) {}

However I'd still prefer doing it this way - too much indention can get kinda annoying:

function change_password($email, $password, $new_password, $confirm_new_password)
{
    if (!$email || !$password || !$new_password || !$confirm_new_password) return false;
    if ($new_password != $confirm_new_password) return false;
    if (!login($email, $password)) return false;
    if (!set_password($email, $new_password)) return false;

    return true;
}
like image 26
Christian Schnorr Avatar answered Sep 25 '22 04:09

Christian Schnorr