PHP Logical Operators Practice Questions with Solutions

Introduction

PHP logical operators are used to combine multiple conditions, reverse a condition, or check whether one or more conditions are true. They are commonly used in student eligibility systems, employee bonuses, login validation, discounts, and business rules. PHP provides logical operators such as && (AND), || (OR), ! (NOT), and, or, and xor. In this chapter, you will practice PHP programs with solutions and sample outputs. PHP Logical Operators Practice Questions with Solutions to help you understand the concepts.

Question 1: Check Student Eligibility Using AND

Problem Statement: A student qualifies for a certificate if they score at least 60 marks and have at least 75% attendance. Use the logical AND operator to check eligibility.

PHP Solution:

<?php
$marks = 80;
$attendance = 85;

$eligible = ($marks >= 60) && ($attendance >= 75);

echo "Eligible for Certificate: ";
var_dump($eligible);
?>

Sample Output:

Eligible for Certificate: bool(true)

Explanation: The && operator returns true only when both conditions are true. The student meets both requirements, so the result is true.

Concepts Covered: Logical AND, comparison operators, Boolean expressions.

Question 2: Check Discount Eligibility Using OR

Problem Statement: A store offers a special discount to customers who are either members or have a purchase amount of at least ₹5,000. Check whether a customer who is not a member but spends ₹6,000 qualifies.

PHP Solution:

<?php
$isMember = false;
$purchaseAmount = 6000;

$eligible = $isMember || ($purchaseAmount >= 5000);

echo "Eligible for Discount: ";
var_dump($eligible);
?>

Sample Output:

Eligible for Discount: bool(true)

Explanation: The || operator returns true when at least one condition is true. The customer is not a member, but the purchase amount meets the required limit.

Concepts Covered: Logical OR, eligibility checks.

Question 3: Reverse a Condition Using NOT

Problem Statement: A product is out of stock when its available quantity is zero. Use the logical NOT operator to determine whether the product is available.

PHP Solution:

<?php
$stock = 10;

$isOutOfStock = ($stock == 0);
$isAvailable = !$isOutOfStock;

echo "Out of Stock: ";
var_dump($isOutOfStock);

echo "Available: ";
var_dump($isAvailable);
?>

Sample Output:

Out of Stock: bool(false)
Available: bool(true)

Explanation: The ! operator reverses a Boolean value. Since the stock is 10, the product is not out of stock and is available.

Concepts Covered: Logical NOT, Boolean reversal.

Question 4: Check Employee Bonus Eligibility Using AND

Problem Statement: An employee receives a performance bonus if their monthly sales are at least ₹100,000 and their performance rating is at least 4. Check an employee with sales of ₹120,000 and a rating of 5.

PHP Solution:

<?php
$monthlySales = 120000;
$rating = 5;

$bonusEligible = ($monthlySales >= 100000)
                 && ($rating >= 4);

echo "Bonus Eligible: ";
var_dump($bonusEligible);
?>

Sample Output:

Bonus Eligible: bool(true)

Explanation: Both conditions must be true for the employee to qualify. The employee meets the sales target and the rating requirement.

Concepts Covered: Logical AND, business rules, comparisons.

Question 5: Check Login Conditions Using AND

Problem Statement: A user can access a dashboard only when both the username and password are correct. Use logical AND to check the login conditions.

PHP Solution:

<?php
$usernameCorrect = true;
$passwordCorrect = false;

$loginAllowed = $usernameCorrect && $passwordCorrect;

echo "Login Allowed: ";
var_dump($loginAllowed);
?>

Sample Output:

Login Allowed: bool(false)

Explanation: The username is correct, but the password is not. Since both conditions must be true, the login check returns false. This example demonstrates a logical check, not a complete authentication system.

Concepts Covered: Logical AND, access conditions, Boolean values.

Question 6: Check Free Delivery Eligibility Using OR

Problem Statement: An online store provides free delivery if the order amount is at least ₹1,000 or the customer has a premium membership. Check an order of ₹700 placed by a premium member.

PHP Solution:

<?php
$orderAmount = 700;
$isPremiumMember = true;

$freeDelivery = ($orderAmount >= 1000)
                || $isPremiumMember;

echo "Free Delivery: ";
var_dump($freeDelivery);
?>

Sample Output:

Free Delivery: bool(true)

Explanation: The order does not reach ₹1,000, but the customer has premium membership. Because at least one condition is true, free delivery is available.

Concepts Covered: Logical OR, order conditions.

Question 7: Understand XOR with Two Conditions

Problem Statement: A company gives a special reward when exactly one of two conditions is true: an employee meets the sales target or has a performance rating of 5. Check an employee who meets the sales target but has a rating of 4.

PHP Solution:

<?php
$meetsSalesTarget = true;
$ratingIsFive = false;

$specialReward = $meetsSalesTarget xor $ratingIsFive;

echo "Special Reward: ";
var_dump($specialReward);
?>

Sample Output:

Special Reward: bool(true)

Explanation: The xor operator returns true when exactly one condition is true. Here, the sales target is met, but the rating is not 5.

Concepts Covered: Exclusive OR, Boolean logic.

Question 8: Check Whether a Number Is Outside a Range

Problem Statement: A system accepts scores from 1 to 100. Use logical OR to check whether a score of 120 is outside the permitted range.

PHP Solution:

<?php
$score = 120;

$isInvalid = ($score < 1) || ($score > 100);

echo "Invalid Score: ";
var_dump($isInvalid);
?>

Sample Output:

Invalid Score: bool(true)

Explanation: A score is invalid if it is below 1 or above 100. The || operator returns true because 120 is greater than 100.

Concepts Covered: Logical OR, range validation, comparisons.

Question 9: Combine AND, OR, and NOT

Problem Statement: A student can join a workshop if they are registered and either have paid the fee or received a fee waiver. Check a registered student who has not paid but has received a waiver.

PHP Solution:

<?php
$isRegistered = true;
$hasPaid = false;
$hasFeeWaiver = true;

$canJoin = $isRegistered
           && ($hasPaid || $hasFeeWaiver);

echo "Can Join Workshop: ";
var_dump($canJoin);
?>

Sample Output:

Can Join Workshop: bool(true)

Explanation: Registration is mandatory. After that, either payment or a fee waiver is sufficient. Parentheses make it clear that the OR condition must be evaluated together before combining it with registration.

Concepts Covered: Combined logical operators, parentheses, eligibility rules.

Question 10: Compare && and and Operator Precedence

Problem Statement: Use the && and and operators in two assignments. Observe how operator precedence changes the results.

PHP Solution:

<?php
$a = true;
$b = false;

$result1 = $a && $b;
$result2 = $a and $b;

echo "Result with &&: ";
var_dump($result1);

echo "Result with and: ";
var_dump($result2);
?>

Sample Output:

Result with &amp;&amp;: bool(false)
Result with and: bool(true)

Explanation: In PHP, && has higher precedence than assignment (=), while and has lower precedence. Therefore:

  • $result1 = $a && $b; assigns the result of the AND expression, which is false.
  • $result2 = $a and $b; assigns $a to $result2 first. The and expression is evaluated afterward, so $result2 remains true.

Use parentheses when you want to make the intended logic explicit.

Concepts Covered: Logical operators, operator precedence, assignment.

Key Takeaways

  • && returns true when both conditions are true.
  • || returns true when at least one condition is true.
  • ! reverses a Boolean value.
  • xor returns true when exactly one operand is true.
  • PHP also supports and and or.
  • && and || have higher precedence than and and or.
  • Parentheses help make complex conditions easier to read.
  • Logical operators are useful for eligibility checks, login conditions, discounts, and validation.
  • Logical expressions commonly return Boolean values: true or false.

Frequently Asked Questions

1. What are logical operators in PHP?

Logical operators combine or reverse conditions. They help a program determine whether one or more conditions are satisfied.

2. What is the difference between && and ||?

&& requires both conditions to be true. || requires at least one condition to be true.

3. What does the NOT operator (!) do?

The ! operator reverses a Boolean value. For example, !true evaluates to false.

4. What is the XOR operator in PHP?

The xor operator returns true when exactly one operand is true. It returns false when both operands have the same Boolean truth value.

5. What is the difference between && and and?

Both represent logical AND, but && has higher operator precedence than and. This difference matters in expressions that include assignment.

6. What is the difference between || and or?

Both represent logical OR, but || has higher precedence than or. Use parentheses to avoid unexpected results in complex expressions.

7. Can PHP logical operators be used with comparison operators?

Yes. For example, ($age >= 18) && ($age <= 60) checks whether an age is between 18 and 60, inclusive.

8. What is short-circuit evaluation?

With &&, PHP skips the second operand if the first is false. With ||, PHP skips the second operand if the first is true. This can prevent unnecessary operations.

9. Where are logical operators used in real PHP projects?

They are used in login checks, form validation, discount eligibility, employee bonuses, student results, and access-control rules.

10. Why should parentheses be used with logical operators?

Parentheses clarify which conditions should be evaluated together. They also help prevent errors caused by operator precedence.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top