Jump to content

Logical if statements.


Samuz

Recommended Posts

Okay i'm trying to compare these sets of variables.

 

code looks like this..

 

if(
               $row->cash > $finalcash &&
               $row->core1 > $finalcore1 &&
               $row->core2 > $finalcore2 &&
               $row->core3 > $finalcore3 &&
               $row->core4 > $finalcore4
               ) {
return true;
}
else {
return false;
}

 

First question is this the appropriate way of doing this?

 

Or would a nested if statement work better?

 

example:

 

if ($row->cash > $finalcash) {
    if ($row->core1 > $finalcore1) {
        if ($row->core2 > $finalcore2) {
            if ($row->core3 > $finalcore3) {
                if ($row->core4 > $finalcore4) {
                    
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
} else {
    return false;
}

 

I know it looks pretty messy, so i'm wondering if anybody is aware of an alternative to achieving this?

Link to comment
Share on other sites

Since it returns a boolean no matter what, you don't even need the if statement.

 

return $row->cash > $finalcash &&
               $row->core1 > $finalcore1 &&
               $row->core2 > $finalcore2 &&
               $row->core3 > $finalcore3 &&
               $row->core4 > $finalcore4;

Link to comment
Share on other sites

Since it returns a boolean no matter what, you don't even need the if statement.

 

return $row->cash > $finalcash &&
               $row->core1 > $finalcore1 &&
               $row->core2 > $finalcore2 &&
               $row->core3 > $finalcore3 &&
               $row->core4 > $finalcore4;

Thanks mate. Nice & fast response. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.