This is often how boolean "or" expressions are evaluated in programming languages. If you have a statement involving functions thus:
if (a() or b()) { ... }
then PHP works out that, if a()
returns true, there is no need to evaluate the second function, since the overall result will be true regardless of the outcome of the second part. We can use this trick as a simple if
mechanism:
(operation_that_might_fail() or report_error());
Here, I've removed the if
around the clause. This will be evaluated just as before - except the result of OR
ing the two is then thrown away, since we don't need to do anything with it.
For this to work, operation_that_might_fail()
must return boolean true on success, and false otherwise. As it happens, many PHP functions do exactly that, so we can often use this approach.
Side note: whilst the statement or
is arguably clearer, PHP programmers tend to prefer the operator ||
. Similarly, and
will do what it says, but &&
is more common.