Zachary J Rollyson
Zachary J Rollyson

Reputation: 153

PHP nested if conditions

What I'm trying to achieve is a nested condition like if ((a = true && b = true) or (c = true && d = true)) {...}. This doesn't seem to be working for me.

To further explain: I have two variables in what we could call a 'top', and two variables in what we could call a 'bottom'. What I need to do is execute code if variables are true in both 'top' and 'bottom'.

To more succinctly illustrate:

if ((t1 = true && b1 = true) or
    (t1 = true && b2 = true) or
    (t2 = true && b1 = true) or
    (t2 = true && b2 = true)
) {
    ...do some stuff...
}

I'm tempted to call it 'conditional statement for lattice problem'... except it isn't really a lattice problem... but it does sort of look like a lattice if you drew it |X| ... s'yeah, you're amazing if you can tell me a good way to do this, and you're super amazing if you can tell me what I ought to call it.

Upvotes: 0

Views: 2083

Answers (3)

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

change condition

if (($t1 == true && $b1 == true) or
($t1 == true && $b2 == true) or
($t2 == true && $b1 == true) or
($t2 == true && $b2 == true)
)

= is assignment operator == is comparison operator.

Upvotes: 0

Dan
Dan

Reputation: 10786

= is used for assignment, == is used to test for equality, === is used to test for equality and equal types. You need to use either == or ===, depending on whether t1, t2, b1, b2 are already boolean or something else. Also the stuff that Adam said, except that or is perfectly valid.

Upvotes: 3

Adam Plocher
Adam Plocher

Reputation: 14233

You need to use two equal signs == instead of =. Also, your variables need to have $ in front of them. And your "or" should be ||.

The logic is ok.

It should look like this:

if (($t1 == true && $b1 == true) ||
    ($t1 == true && $b2 == true) ||
    ($t2 == true && $b1 == true) ||
    ($t2 == true && $b2 == true)
) {
    ...do some stuff...
}

Upvotes: 2

Related Questions