EdzJohnson
EdzJohnson

Reputation: 1175

Very basic PHP - IF AND IF...?

My current code is as follows:

if ( ( $status == 'active' ) || 
     ( $status == 'full' ) ) {

I need to also include an AND statement. So if $status is either full or active AND $position matches 'need photo' or 'completed' then it displays. How do I include an AND statement?

I tried the following but it didn't seem to work:

if ( ( $status == 'active' ) || 
     ( $status == 'full' ) && 
     ( $position == 'need photo' ) || 
     ( ( $position == 'completed' ) ) {

Any help? Thank you! :-) I'm fairly new to all of this. I tried Google but couldn't find a clear answer.

Upvotes: 0

Views: 56

Answers (3)

Counterfly
Counterfly

Reputation: 29

I think you are just missing some brackets. What you want is if ((A) && (B)), where A and B are complex expressions (an expression containing two sub-expressions).

In your case: A = ( $status == 'active' ) || ( $status == 'full' ) and B = ( $position == 'need photo' ) || ( $position == 'completed' )

So, try this: if ( **(** ( $status == 'active' ) || ( $status == 'full' ) **)** && **(** ( $position == 'need photo' ) || ( $position == 'completed' ) **)** ) {

Upvotes: 0

Ed Gibbs
Ed Gibbs

Reputation: 26343

According to the PHP documentation on operator precedence, AND takes precedence over OR, so you need to group the OR expressions with parentheses:

if ( ($status == 'active || $status == 'full) && ($position == 'need photo' || $position == 'completed') ) {
    ...

Upvotes: 1

Paul
Paul

Reputation: 141827

&& has higher precedence than || so the code you tried is the same as:

if ($status == 'active' || ($status == 'full' && $position == 'need photo') || $position == 'completed') {

Which in plain English means, if either status is active, or both status is full and position is need photo, or position is completed.

But you want:

if (($status == 'active' || $status == 'full') && ($position == 'need photo' || $position == 'completed')) {

Which means, if either status is active or status is full, and either position is need photo or position is completed.

Upvotes: 3

Related Questions