Jump to content

Recommended Posts

Hey,

 

I have a PHP statement which basically says:

 

variableA = 1 OR variableA = 2 AND variableB = Z AND variableC = N OR variableC = M

 

actual statement is below, but the above is easier to read!:

$where = "location = 'ALL NORTH' OR location = 'Andreas' OR location = 'Ballaugh' OR location = 'Bride' OR location = 'Jurby' OR location = 'Lezayre' OR location = 'Maughold' OR location = 'Ramsey' OR location = 'St Judes' OR location = 'Sulby' AND sales_types_available LIKE '%Buy%' AND show_on_website = '1' AND development_type = 'HOUSING' OR development_type = '3 Bed House' OR development_type = '4 Bed House' OR development_type = '5 Bed House +' OR development_type = '1 Bed Bungalow' OR development_type = '2 Bed Bungalow' OR development_type = '3 Bed Bungalow' OR development_type = '4 Bed Bungalow'";

 

the above statement results in variable A being ignored if varaible C matches - because of the OR

i.e

Variable A = 1 or 2

AND Varaible B = Z

OR Variable C = M or N

 

 

what I want is variableA to apply, and then variableC to apply seperately

so:

Variable A = 1 or 2

AND Varaible B = Z

AND Variable C = M or N

 

is that possible? or do i need to split it down to two statements?

 

Ta

Link to comment
https://forums.phpfreaks.com/topic/180187-if-statement-or-and-or/
Share on other sites

More generally, AND takes higher precedence than OR. This means that the two following statements are equivalent:

a OR b AND c

a OR (b AND c)

 

Both of them are also left associative (that means they're evaluated from left to right), which means that when you write:

variableA = 1 OR variableA = 2 AND variableB = Z AND variableC = N OR variableC = M

The implicit parentheses are like this:

(variableA = 1 OR ((variableA = 2 AND variableB = Z) AND variableC = N)) OR variableC = M

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.