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
https://forums.phpfreaks.com/topic/253992-logical-if-statements/
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;

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

Archived

This topic is now archived and is closed to further replies.

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