PHP - Logical Operators

Logical operators in PHP are used to combine multiple conditions in a conditional statement. They return either true or false depending on the evaluation of the conditions. These operators are commonly used in if statements, loops, and other control structures.

In this guide, we will explore different logical operators in PHP with practical examples.


List of PHP Logical Operators

PHP provides the following logical operators:

OperatorSymbolDescriptionExampleResult
AND&& or andReturns true if both conditions are true($a == 10 && $b == 20)true if both conditions are true
OR`oror`Returns true if at least one condition is true
NOT!Returns true if the condition is false!($a == 10)true if $a is NOT 10
XORxorReturns true if only one condition is true($a == 10 xor $b == 30)true if only one condition is true

Examples of PHP Logical Operators

1. AND Operator (&& or and)

The AND operator returns true only if both conditions are true.

<?php $a = 10; $b = 20; if ($a == 10 && $b == 20) { echo "Both conditions are true"; } else { echo "One or both conditions are false"; } // Output: Both conditions are true ?>

2. OR Operator (|| or or)

The OR operator returns true if at least one condition is true.

<?php $a = 10; $b = 30; if ($a == 10 || $b == 20) { echo "At least one condition is true"; } else { echo "Both conditions are false"; } // Output: At least one condition is true ?>

3. NOT Operator (!)

The NOT operator reverses the result of a condition.

<?php $a = 10; if (!($a == 20)) { echo "Condition is false, so NOT makes it true"; } else { echo "Condition is true"; } // Output: Condition is false, so NOT makes it true ?>

4. XOR Operator (xor)

The XOR operator returns true if only one of the conditions is true. If both are true or both are false, it returns false.

<?php $a = 10; $b = 30; if ($a == 10 xor $b == 30) { echo "Only one condition is true"; } else { echo "Either both are true or both are false"; } // Output: Either both are true or both are false ?>

Practical Use Cases of Logical Operators

1. User Login System

<?php $username = "admin"; $password = "12345"; if ($username == "admin" && $password == "12345") { echo "Login successful!"; } else { echo "Invalid credentials!"; } ?>

2. Age Restriction Check

<?php $age = 18; if ($age >= 18 && $age <= 60) { echo "You are eligible to apply"; } else { echo "You are not eligible"; } ?>

3. Checking User Roles

<?php $role = "editor"; if ($role == "admin" || $role == "editor") { echo "Access granted"; } else { echo "Access denied"; } ?>

Key Takeaways

  1. Use && for strict conditions where both must be true.
  2. Use || when at least one condition should be true.
  3. Use ! to negate conditions.
  4. Use xor when exactly one condition must be true.
  5. Logical operators help in decision-making in PHP applications.

Conclusion

PHP logical operators are essential for handling complex conditions in your applications. They are widely used in authentication systems, form validation, and other logical operations.

Understanding these operators will help you write efficient and secure PHP code. Start practicing with real-world examples to improve your PHP skills!