Operator precedence or how does 'or die()' work.
In the old school PHP code you can often see a statement like this:
$result = some_function() OR some_other_function();
(sadly, but most of time it features
die()
assome_other_function()
, which is extremely bad practice. Every time you see it, replacedie()
withtrigger_error()
, which will give you exactly the same result on your local system but will make the code ready for going live. Read more on the proper error reporting...)
In this statement, some_other_function()
would be called only if some_function()
fails (or, strictly speaking, returns an empty result).
But how does it actually work? Here you go:
A smart interpreter
A code like this
some expression 1 OR some expression 2;
is just a PHP expression. Exactly the same as
$variable1 * $variable1;
and OR
operator is just like *
(or any other - >
, =
, .
and such) operator.
Most of time we are using the result OR
operator in some context, like in the conditional operator
if (some expression 1 OR some expression 2) ...
But if
is not obligatory here. We can always omit it, leaving only
some expression 1 OR some expression 2;
and it will cause no parse error.
And here comes the key part: because OR
operator will return TRUE if one of the 2 operands is TRUE, a smart interpreter wouldn't run the second operand at all, if first one already evaluated to TRUE!
So, this is the trick:
some expression 1 OR some expression 2 ;
means "execute some expression 2
only if some expression 1
returned FALSE
"
Operator precedence
Now let's try assign the result of OR
operator to some variable
$variable = some expression 1 OR some expression 2;
There is a thing called operator precedence. And =
operator has a higher precedence than OR
and so it will be executed first. Thus, this expression can be written as
($variable = some expression 1) OR some expression 2;
So now you can conclude how the whole thing works
some expression 1
gets evaluated first.- then
=
operator gets executed, and the result ofsome expression 1
assigned to$variable
. - then PHP proceeds to the execution of
OR
statement and here goes the trick:- if value of $variable is equal to
TRUE
, then PHP won't fire the rightmost one. - but if it is equal to
FALSE
, the rightmost expression gets executed and we have an error triggered (iftrigger_error()
is used as some expression 2).
- if value of $variable is equal to
Note that logical operators in PHP can be used with any data type, not only Boolean values, due PHP's automatic type conversion:
- FALSE would be assumed for
0
,"0"
(a string contains literal 0),NULL
and empty array. - TRUE would be assumed for everything else, including values like
-1
," "
(a space character),array(0)
(an array with a single item equal to 0).
The difference between || and OR
On a side note, now you can tell why we are using ||
instead of OR
when we want a boolean result but not the trick discussed above. ||
has lesser precedence than =
and thus
$variable = some expression 1 || some expression 2;
will be executed the straight way - first we are getting the Boolean result of ||
and then it gets assigned to $variable
. But of course we can always use ()
braces to manage precedence manually - as braces has the highest precedence of them all, everything inside will be evaluated first.
Related articles:
- Relative and absolute paths, in the file system and on the web server.
- PHP error reporting
- Do you really need to check for both isset() and empty() at the same time?
- What's wrong with popular articles telling you that foo is faster than bar?
- MVC in simpler terms or the structure of a modern web-application
- How to get a single player's rank based on the score
- Articles
- Do you abuse the null coalescing operator (and isset/empty as well)?
- Why should I use prepared statements if escaping is safe?
- Numerical strings comparison
- Do generators really reduce the memory usage?
- How to make Stack Overflow a nice place
- Iterating over array getting look-ahead items along
- How to debug small PHP programs
Add a comment
Please refrain from sending spam or advertising of any sort.
Messages with hyperlinks will be pending for moderator's review.
Markdown is now supported:
>
before and an empty line after for a quote