0

How would the valid syntax be for the following if-statement?

if ($properties->offering_type === 'y' || $properties->offering_type === 'p' && $properties->sold != 'y') { 
  // echo something
} else {
}

I want to echo something when offering_type is either y or p and sold is not y

0

2 Answers 2

3

&& has higher precedence than ||, so your condition is interpreted as

if ($properties->offering_type === 'y' || 
    ($properties->offering_type === 'p' && $properties->sold != 'y')) { 

You need to add parentheses to group the || together.

if (($properties->offering_type === 'y' || $properties->offering_type === 'p')
    && $properties->sold != 'y') { 
Sign up to request clarification or add additional context in comments.

2 Comments

A well explained answer:).
Indeed well explained!
1
<?php

if ( ($properties->offering_type === 'y' || $properties->offering_type === 'p') && ($properties->sold != 'y') ) { 
  // echo something
} 
else {

}

Please note that you are using === which means the type should also be the same. And you are not doing that for the sold property (!=).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.