Using AND/OR in if else PHP statement Using AND/OR in if else PHP statement php php

Using AND/OR in if else PHP statement


Yes. The answer is yes.
http://www.php.net/manual/en/language.operators.logical.php


Two things though:

  • Many programmers prefer && and || instead of and and or, but they work the same (safe for precedence).
  • $status = 'clear' should probably be $status == 'clear'. = is assignment, == is comparison.


There's some joking, and misleading comments, even partially incorrect information in the answers here. I'd like to try to improve on them:

First, as some have pointed out, you have a bug in your code that relates to the question:

if ($status = 'clear' AND $pRent == 0)

should be (note the == instead of = in the first part):

if ($status == 'clear' AND $pRent == 0)

which in this case is functionally equivalent to

if ($status == 'clear' && $pRent == 0)

Second, note that these operators (and or && ||) are short-circuit operators. That means if the answer can be determined with certainty from the first expression, the second one is never evaluated. Again this doesn't matter for your debugged line above, but it is extremely important when you are combining these operators with assignments, because

Third, the real difference between and or and && || is their operator precedence. Specifically the importance is that && || have higher precedence than the assignment operators (= += -= *= **= /= .= %= &= |= ^= <<= >>=) while and or have lower precendence than the assignment operators. Thus in a statement that combines the use of assignment and logical evaluation it matters which one you choose.

Modified examples from PHP's page on logical operators:

$e = false || true;

will evaluate to true and assign that value to $e, because || has higher operator precedence than =, and therefore it essentially evaluates like this:

$e = (false || true);

however

$e = false or true;

will assign false to $e (and then perform the or operation and evaluate true) because = has higher operator precedence than or, essentially evaluating like this:

($e = false) or true;

The fact that this ambiguity even exists makes a lot of programmers just always use && || and then everything works clearly as one would expect in a language like C, ie. logical operations first, then assignment.

Some languages like Perl use this kind of construct frequently in a format similar to this:

$connection = database_connect($parameters) or die("Unable to connect to DB.");

This would theoretically assign the database connection to $connection, or if that failed (and we're assuming here the function would return something that evalues to false in that case), it will end the script with an error message. Because of short-circuiting, if the database connection succeeds, the die() is never evaluated.

Some languages that allow for this construct straight out forbid assignments in conditional/logical statements (like Python) to remove the amiguity the other way round.

PHP went with allowing both, so you just have to learn about your two options once and then code how you'd like, but hopefully you'll be consistent one way or another.

Whenever in doubt, just throw in an extra set of parenthesis, which removes all ambiguity. These will always be the same:

$e = (false || true);$e = (false or true);

Armed with all that knowledge, I prefer using and or because I feel that it makes the code more readable. I just have a rule not to combine assignments with logical evaluations. But at that point it's just a preference, and consistency matters a lot more here than which side you choose.


A bit late but don't matter...
the question is "How do you use...?" short answer is you are doing it correct


The other question would be "When do you use it?"
I use && instead of AND and || instead of OR.

$a = 1$b = 3

Now,

if ($a == 1 && $b == 1) { TRUE } else { FALSE }

in this case the result is "FALSE" because B is not 1, now what if

if ($a == 1 || $b == 1) { TRUE } else { FALSE }

This will return "TRUE" even if B still not the value we asking for, there is another way to return TRUE without the use of OR / || and that would be XOR

if ($a == 1 xor $b == 1) { TRUE } else { FALSE }

in this case we need only one of our variables to be true BUT NOT BOTH if both are TRUE the result would be FALSE.

I hope this helps...

more in:
http://www.php.net/manual/en/language.operators.logical.php