PHP Lessons 10 – Logical Operators

Last updated on November 27th, 2023

What are Logical operators in PHP?

Logical operators in PHP are used to perform logical operations on boolean values. There are only two boolean values: true and false. Logical operators allow to combine and manipulate boolean values to make decisions and control the flow of the PHP code.

What types of logical operators are there?

There are 3 main types of logical operators:

1. AND Operator (&&)
The AND operator, represented by &&, returns true only if both the left and right expressions are true. It can be used to combine two or more conditions, and the result is true only if all conditions are met.

2. OR Operator (||)
The OR operator, represented by ||, returns true if at least one of the left or right expressions is true. It can be used to combine multiple conditions, and the result is true if any of the conditions is met.

3. NOT Operator (!)
The NOT operator, represented by !, is a unary operator that negates or reverses the value of an expression. If the expression is true, ! makes it false, and vice versa. It can be used to check for the opposite of a condition.

Examples of using logical operators

1. AND (&&) Operator:

<?php
  $isWeekend = true;
  $isSunny = true;

  if ($isWeekend && $isSunny) {
    echo "Let's have a picnic!";
  } else {
    echo "We'll find something else to do.";
  }
?>

Result: The script will display “Let’s have a picnic!”

In this example, we check if it’s the weekend ($isWeekend) AND if it’s sunny ($isSunny). Since both conditions are true, it suggests having a picnic.

2. OR (||) Operator:

<?php
  $isWeekend = true;
  $isHoliday = false;

  if ($isWeekend || $isHoliday) {
  echo "It's either the weekend or a holiday!";
  } else {
  echo "It's a regular workday.";
  }
?>

Result: The script will display “It’s either the weekend or a holiday!”.

In this example we have two boolean variables, $isWeekend and $isHoliday, representing whether it’s the weekend or a holiday. The if statement checks if either $isWeekend is true (meaning it’s the weekend) OR $isHoliday is true (meaning it’s a holiday). If either condition is met, it prints “It’s either the weekend or a holiday!”. If neither condition is met, it falls into the else block and prints “It’s a regular workday.”

3. NOT (!) Operator:

Let’s have the following code:

<?php
  $isRainy = true;

  if ($isRainy) {
  echo "It's raining, take an umbrella.";
  } else {
  echo "It's not raining, you don't need an umbrella.";
  }
?>

Result: The script will display “It’s raining, take an umbrella.”, because the condition is true ($isRainy).

Now let’s use the NOT (!) operator by putting an exclamation mark in front of the variable $isRainy in if statement:

<?php
  $isRainy = true;

  if (!$isRainy) {
  echo "It's raining, take an umbrella.";
  } else {
  echo "It's not raining, you don't need an umbrella.";
  }
?>

In this case, the result will be: “It’s not raining, you don’t need an umbrella.” because the operator reverses the value of the variable (from true to false).

The NOT operator (!) is used to reverse the value of $isRainy, making it easier to handle conditions where you want to check the opposite of a given condition.