Curriculum
This tutorial will teach you about the PHP OR operator (||) and how to use it to construct complex logical expressions.
The logical OR operator takes two operands and returns true if both are true; otherwise, it returns false. In other words, if both operands are false, the logical OR operator returns false.
PHP uses either the or keyword or the || to represent the logical OR operator, as shown below:
expression1 or expression2
or
expression1 || expression2
The following table shows the outcome of the or operator:
expression1 | expression2 | expression1 || expression2 |
---|---|---|
true |
true |
true |
true |
false |
true |
false |
true |
true |
false |
false |
false |
Because PHP keywords are case-insensitive, the or, Or, and OR are all the same.
The operators || and or produce the same result. The only distinction between the || and or operators is their order of precedence. The or operator takes precedence over the || operator.
Assume you need to clear the website’s cache if the flags $expired or $purge are set to true. To do so, use the logical OR operator as follows:
<?php $expired = true; $purged = false; $clear_cache = $expired || $purged; var_dump($clear_cache);
Output
bool(true)
Because $expired is true, the OR operator result is also true.
However, if you change the $expired to false, the result will be false, as shown in the example below:
<?php $expired = false; $purged = false; $clear_cache = $expired || $purged; var_dump($clear_cache);
In practise, the logical OR operator is frequently used in if, if-else, if-elseif, while, and do-while statements.
The logical OR operator knows that if the first operand is true, the result must also be true. It does not evaluate the second operand in this case. This is known as short-circuiting.
In practise, the or operator is frequently used in the following pattern:
function_call() || die(message)
If function call() returns true, the call was successful. The second operand, a call to the die() function, will never be executed by PHP. Otherwise, PHP will issue an error message and call the die() function.
As an example:
<?php function connect_to_db() { return false; } connect_to_db() || die('Cannot connect to the database.');
Output
Cannot connect to the database
When the connect to db() function returns false in this example, PHP calls the die() function, which displays the error message.
Consider the following:
<?php $result = false or true; var_dump($result);
Output
bool(false)
In this case, you’d expect the $result to be true because the false or true expression returns true. This, however, is not the case.
Consider the following statement when evaluating it:
$result = false or true;
PHP evaluates the $result = false first, followed by the or operator, because the = operator takes precedence over the or operator.
It is technically equivalent to the following:
($result = false) or true;
As a result, the false value is assigned to $result.
To correct this, use parentheses to change the order of evaluation:
<?php $result = (false or true); var_dump($result);
Output
bool(true)
You can also use the || operator:
<?php $result = false || true; var_dump($result);
Output
bool(true)
As a result, it’s best to always use the || operator instead of the or operator.