Web20 University

How to use PHP OR in if Statement

In PHP, you can use the logical OR operator (|| or or) in an if statement to check if at least one of the conditions is true. If any of the conditions combined with the OR operator is true, the code within the if block will be executed.

Here’s an example using the || OR operator:

$age = 25;
$membership = "premium";

if ($age >= 18 || $membership == "premium") {
    echo "You have access to the content.";
} else {
    echo "You don't have access to the content.";
}

In this example, a user will have access to the content if they are 18 years old or older, or if they have a “premium” membership. If either of these conditions is true, the message “You have access to the content.” will be displayed.

Here’s the same example using the or OR operator:

$age = 25;
$membership = "premium";

if ($age >= 18 or $membership == "premium") {
    echo "You have access to the content.";
} else {
    echo "You don't have access to the content.";
}

Both || and or can be used as OR operators, but they have different precedence. The || operator has higher precedence than or. This difference might affect the order of evaluation in complex expressions. In most cases, || is the preferred choice for logical OR operations in PHP.