There are some jokes and misleading comments, even partially incorrect information in the answers here. I would like to try to improve them:
First , as some have pointed out, you have a bug in the code related to the question:
if ($status = 'clear' AND $pRent == 0)
should be (pay attention to == instead of = in the first part):
if ($status == 'clear' AND $pRent == 0)
which in this case is functionally equivalent
if ($status == 'clear' && $pRent == 0)
Second , note that these operators ( and or && || ) are short-circuit operators. This means that if the answer can be determined with certainty from the first expression, the second will never be evaluated. Again, this does not matter for your debugged line above, but it is extremely important when you combine these operators with the destination, because
Third , the real difference between and or and && || is their operator priority . In particular, it is important that && || had higher priority than the assignment operators ( = += -= *= **= /= .= %= &= |= ^= <<= >>= ), and and or had lower precision than the assignment operators . Thus, in a statement that combines the use of assignment and logical evaluation, it is important which one to choose.
Modified examples from the PHP page for logical operators :
$e = false || true;
will evaluate to true and assign this value $e , because || has a higher operator precedence than = , and therefore it essentially evaluates the following:
$e = (false || true);
However
$e = false or true;
assigns false to $e (and then performs the or operation and evaluates to true ), since = has a higher operator priority than or , essentially evaluating as follows:
($e = false) or true;
The fact that this ambiguity even exists allows many programmers to always use && || and then everything works clearly, as one would expect in a language such as C, i.e. logical operations first, then assignment.
Some languages, such as Perl, use this construct often in a format like this:
$connection = database_connect($parameters) or die("Unable to connect to DB.");
This would theoretically assign a connection to the database $connection , or if it didnβt work (and we assume that the function returns a value here, which in this case will be false ), it will end the script with an error message. Due to a short circuit, if the database connection succeeds, die() never evaluated.
Some languages ββthat allow this construction explicitly prohibit assignments in conditional / logical operations (e.g. Python) to eliminate bias in the opposite direction.
PHP went by resolving both, so you just need to find out about your two options once, and then the code as you want, but hopefully you will be consistent anyway.
Whenever in doubt, just add an extra set of brackets that removes all the ambiguity. They will always be the same:
$e = (false || true); $e = (false or true);
Armed with all this knowledge, I prefer to use and or , because I feel that it makes the code more readable. I just have a rule not to combine assignments with logical ratings. But at this moment it is just a preference, and the sequence here is much more important than which one you choose.