PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Logical operators in PHP are used to combine conditional statements. They allow you to perform logical operations, which are crucial in decision-making processes in your code. Here is an in-depth look at the logical operators in PHP:

1. AND (&& and and):

  • The ‘&&‘ (logical AND) operator returns true if both operands are true. It evaluates expressions from left to right and stops as soon as it finds one that is false (short-circuit evaluation).
  • The ‘and‘ operator works similarly but has a lower precedence than ‘&&‘.
				
					$a = true;
$b = true;
$c = false;

// Using &&
if ($a && $b) {
    echo "Both are true\n"; // This will be printed
}

if ($a && $c) {
    echo "One is false\n"; // This will not be printed
}

// Using and
if ($a and $b) {
    echo "Both are true\n"; // This will be printed
}

if ($a and $c) {
    echo "One is false\n"; // This will not be printed
}

				
			

2. OR (|| and or):

  • The ‘||‘ (logical OR) operator returns true if at least one of the operands is true. It also uses short-circuit evaluation, stopping as soon as it finds a true operand.
  • The ‘or‘ operator works similarly but has a lower precedence than ‘||‘.
				
					$a = true;
$b = false;

// Using ||
if ($a || $b) {
    echo "At least one is true\n"; // This will be printed
}

if ($b || $c) {
    echo "Both are false\n"; // This will not be printed
}

// Using or
if ($a or $b) {
    echo "At least one is true\n"; // This will be printed
}

if ($b or $c) {
    echo "Both are false\n"; // This will not be printed
}

				
			

3. XOR (xor):

  • The ‘xor‘ (exclusive OR) operator returns true if one and only one of the operands is true.
				
					$a = true;
$b = false;

// Using xor
if ($a xor $b) {
    echo "One is true, the other is false\n"; // This will be printed
}

if ($a xor $a) {
    echo "Both are true\n"; // This will not be printed
}

				
			

4. NOT (!)

  • The ‘!‘ (logical NOT) operator negates the truth value of its operand.
				
					$a = true;
$b = false;

// Using !
if (!$b) {
    echo "b is false\n"; // This will be printed
}

if (!$a) {
    echo "a is false\n"; // This will not be printed
}

				
			

Operator Precedence

Precedence determines how operators are parsed concerning each other. Operators with higher precedence are evaluated before operators with relatively lower precedence.

In PHP, the precedence of logical operators from highest to lowest is:

  1. ! (NOT)
  2. && (AND)
  3. || (OR)
  4. xor (exclusive OR)
  5. and (AND, lower precedence)
  6. or (OR, lower precedence)

Short-circuit Evaluation

  • Short-circuit evaluation means that the evaluation stops as soon as the result is determined.
  • For example, in the expression ‘$a && $b‘, if ‘$a‘ is false, the whole expression will be false regardless of ‘$b‘, so ‘$b‘ is not evaluated.

Practical Example

  • Combining logical operators in real-world scenarios often involves complex conditional statements. Consider an example of user authentication:
				
					$user_logged_in = true;
$user_has_permission = false;
$is_admin = false;

if ($user_logged_in && ($user_has_permission || $is_admin)) {
    echo "Access granted\n";
} else {
    echo "Access denied\n"; // This will be printed
}

				
			

In this example, access is granted only if the user is logged in and has permission or is an admin. The use of parentheses ensures that the || operation is evaluated first due to its higher precedence over &&.

Scroll to Top