PHP OR Operator

Created with Sketch.

PHP OR Operator

Summary: in this tutorial, you’ll learn about the PHP OR operator (||) and how to use it to build complex logical expressions.

Introduction to the PHP OR operator

The logical OR operator accepts two operands and returns true if either operand is true; otherwise, it returns false. In other words, the logical OR operator returns false if both operands are false.

To represent the logical OR operator, PHP uses either the or keyword or the || as follows:

expression1 or expression2

 

Or

expression1 || expression2

 

The following table illustrates the result of the or operator:

expression1expression2expression1 || expression2
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Note that the or, Or, and OR are the same because PHP keywords are case-insensitive.

The || and or operators return the same result. The only difference between the || and or operators are their precedences. The or operator has higher precedence than the || operator.

PHP OR operator examples

Suppose that you need to clear the cache of the website if the flag $exprired or $purge is set to true. To do that, you can use the logical OR operator as follows:

<?php

$expired = true;
$purged = false;

$clear_cache = $expired || $purged;

var_dump($clear_cache);

Code language: HTML, XML (xml)

Output:

bool(true)

Code language: JavaScript (javascript)

Since $expired is true, the result of the OR operator is also true.

However, if you change the $expired to false, the result will be false as shown in the following example:

<?php

$expired = false;
$purged = false;

$clear_cache = $expired || $purged;

var_dump($clear_cache);

Code language: HTML, XML (xml)

In practice, you often use the logical OR operator in the if, if-else, if-elseif, while, and do-while statements.

Short-circuiting

When the first operand is true, the logical OR operator knows that the result must be also true. In this case, it doesn’t evaluate the second operand. This process is called short-circuiting.

In practice, you often find that the or operator is used in the following pattern:

function_call() || die(message)

Code language: PHP (php)

If the function_call() returns true, it succeeded. PHP will never execute the second operand which is a call to the die() function. Otherwise, PHP will call the die() function with an error message.

For example:

<?php

function connect_to_db()
{
return false;
}

connect_to_db() || die('Cannot connect to the database.');

Code language: HTML, XML (xml)

Output:

Cannot connect to the database

 

In this example, the connect_to_db() function returns false, PHP calls the die() function that shows the error message.

The PHP OR gotchas

See the following example:

<?php

$result = false or true;

var_dump($result);

Code language: HTML, XML (xml)

Output:

bool(false)

Code language: JavaScript (javascript)

In this example, you would expect that the $result is true because false or true expression returns true. However, it is not the case.

When evaluating the following statement:

$result = false or true;

Code language: PHP (php)

PHP evaluates the $result = false first and then the or operator second because the = operator has higher precedence than the or operator.

Notice that each operator has precedence. And PHP will evaluate the operators with the higher precedence before the ones with the lower precedence.

Technically, it is equivalent to the following:

($result = false) or true;

Code language: PHP (php)

Therefore, $result is assigned the false value.

To fix this, you need to use parentheses to change the order of evaluation:

<?php

$result = (false or true);

var_dump($result);

Code language: HTML, XML (xml)

Output:

bool(true)

Code language: JavaScript (javascript)

Or you can use the || operator:

<?php

$result = false || true;
var_dump($result);

Code language: HTML, XML (xml)

Output:

bool(true)

Code language: JavaScript (javascript)

Therefore, it’s a good practice to always use the || operator instead of the or operator.

Summary

  • Use the PHP OR operator (or, ||) to combine two expressions and returns true if either expression is true; otherwise, it returns false.
  • The logical OR operator is short-circuiting.
  • Do use the || operator instead of the or operator.

Leave a Reply

Your email address will not be published. Required fields are marked *