php

Understanding the OR Operator in PHP

In programming, operators play a vital role in performing specific operations on variables and values. PHP, like many other programming languages, utilizes operators that are classified into distinct groups, such as assignment operators, logical operators, and comparison operators. Among these operators, the OR logical operator gives true output if at least one condition is true.

Learn more about the PHP OR operator in the following sections.

PHP OR Operator

In PHP, the OR operator takes the two conditions and returns true if any one of the conditions is true, otherwise, it returns false. The syntax of the OR operator in PHP is:

condition1 || condition2

Or you can also write the OR operator as follows:

condition1 or condition2

condition1 OR condition2

Where condition1 and condition2 are the two conditions to be evaluated. The OR operator will evaluate condition1, and if condition1 is true, it will return true without evaluating condition2. If condition1 is false, it will evaluate condition2 present on the right side of the OR operator, and if it is true, the OR operator will return true. If both condition1 and condition2 are not true, the operator will return a false value.

It is worth noting that PHP is a case-insensitive language, which means that you can use the or keyword or the OR operator interchangeably.

The below-mentioned table displays the result of the OR operator when applied to two conditions:

Condition 1 Condition 2 Result
True True True
True False True
False True True
False False False

Examples

Here are a few examples that demonstrate the working of the OR operator in PHP.

Example 1: You can use the OR operator in PHP to check whether a number is greater or less than another number. Here is an example code that illustrates this process:

<?php

$num1 = 15;
$num2 = 8;
$num3 = 4;
if ($num1 > $num2 || $num1 < $num3) {
  echo "Number 1 is greater than Number 2 and smaller than Number 3";
}
else
{
  echo "Conditions not met";
}
?>

In the above example, the OR operator is used to check whether the variable num1 is either greater than num2 or less than num3. If either of these conditions is true, the message ” Number 1 is greater than Number 2 and smaller than Number 3″ will be displayed. Otherwise, the else statement’s instruction will be executed.

Output

Example 2: You can also use the OR operator to check the existence of an element in an array. The below-written program is an example of such a scenario.

<?php

$names = array("Hardy", "Mat", "Tom", "Kasper");

if (in_array("Hardy", $names) OR in_array("Mat", $names)) {
  echo "Hardy or Mat is in the array of names";
}
else
{
  echo "Hardy or Mat is not in the array of names";
}

?>

In this example, the in_array() function is used to check whether a name is present in the names array. The OR operator is used to check whether the name “Hardy” or the name “Mat” is present in the array.

If either of these conditions is true, the message “Hardy or Mat is in the array of names” will be displayed.

Output

If we modify the code:

<?php

$names = array("Hardy", "Mat", "Tom", "Kasper");

if (in_array("Sam", $names) OR in_array("Alex", $names)) {
  echo "Hardy or Mat is in the array of names";
}
else
{
  echo "Hardy or Mat is not in the array of names";
}

?>

In this case, both conditions are false and the else’s statement will be displayed.

Output

Conclusion

In PHP, the OR operator is a logical operator which is used to combine multiple conditions. If any of the conditions are true, this operator returns the true value else it returns the false value. The logical operator in PHP is also known as short-circuiting because it does not evaluate the second condition if the first condition is true. We have discussed the OR operator with examples in the above section of this guide.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.