amanxman Posted November 3, 2009 Share Posted November 3, 2009 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 Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted November 3, 2009 Share Posted November 3, 2009 learn about parenthesis it's just like doing high school algebra. to use your examples (((Variable A=1 or Variable A=2) and Variable B = Z) or (Variable C=M or Variable C= N)) Quote Link to comment Share on other sites More sharing options...
amanxman Posted November 3, 2009 Author Share Posted November 3, 2009 legend, you've just saved me hours of work... and so simple thanks Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 3, 2009 Share Posted November 3, 2009 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.