05-08-2009, 05:38 PM
|
#15 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by sketchMedia
In PHP AND and OR operate under different precedence rules to || and &&.
http://uk3.php.net/manual/en/languag...precedence.php
An example:
PHP Code:
$e = false || true;
$f = false or true;
On the face of it, it seems like both operate the same and thus both evaluate to ' true' however this wont behave as expected, and $f will be assigned ' false', why?
If you look at the precedence chart on php.net, you see that ' or' is lower down the list than ' ||' this means that it has lower precedence, this in itself isn't the issue however.
The issue arises when you use it in conjunction with another operator, for example ' ='. '||' is higher in the list than ' or', so therefore any expression with ' =' in it must be evaluated first, thus:
PHP Code:
$f = false or true;
//php interprets as
($f = false) or true;
Hope that clarifies it.
|
Wow, what a great find Sketch! I tried this out just now and you are correct...this is almost an easter egg....
In PHP, I always use '||', I never use OR or AND surprisingly so I hope that by using '||' and '&&', I won't ever run into any issues that spits out the wrong results...
How come when something evals to true, you get 1, but when it evals to false, you don't get any output? I thought a '0' would be the output?
The output of your code above would be:
e: 1
f:
|
|
|
|