Introduction
PHP comparison operators are used to compare two values and determine whether they are equal, different, greater, or smaller. These operators return a Boolean result: true or false. They are commonly used in student result systems, employee salary comparisons, product pricing, and login validation. In this chapter, you will practice PHP programs covering equality, strict comparison, greater than, less than, and other comparison operators. PHP Comparison Operators Practice Questions with Solutions to help you understand the concepts.
Question 1: Check Whether Two Numbers Are Equal
Problem Statement: Create two variables containing numbers. Use the equality operator to check whether both numbers are equal.
PHP Solution:
<?php
$number1 = 100;
$number2 = 100;
$result = $number1 == $number2;
echo "Are both numbers equal? ";
var_dump($result);
?>
Sample Output:
Are both numbers equal? bool(true)
Explanation: The == operator checks whether two values are equal. Because both variables contain 100, the comparison returns true.
Concepts Covered: Equality operator, Boolean results.
Question 2: Check Whether Two Values Are Not Equal
Problem Statement: A product has an original price of ₹500 and a discounted price of ₹450. Check whether the two prices are different.
PHP Solution:
<?php
$originalPrice = 500;
$discountedPrice = 450;
$isDifferent = $originalPrice != $discountedPrice;
echo "Are the prices different? ";
var_dump($isDifferent);
?>
Sample Output:
Are the prices different? bool(true)
Explanation: The != operator returns true when two values are not equal. Since ₹500 and ₹450 are different, the result is true.
Concepts Covered: Not-equal operator, price comparison.
Question 3: Compare Two Values Using the Strict Equality Operator
Problem Statement: Store 50 as an integer and "50" as a string. Compare them using both == and ===.
PHP Solution:
<?php
$number = 50;
$textNumber = "50";
echo "Loose Equality: ";
var_dump($number == $textNumber);
echo "Strict Equality: ";
var_dump($number === $textNumber);
?>
Sample Output:
Loose Equality: bool(true)
Strict Equality: bool(false)
Explanation: The == operator compares values with applicable type conversion. The === operator checks both value and data type. Since one variable is an integer and the other is a string, strict equality returns false.
Concepts Covered: ==, ===, data type comparison.
Question 4: Check Whether One Number Is Greater Than Another
Problem Statement: Two employees receive monthly salaries of ₹45,000 and ₹38,000. Check whether the first employee earns more than the second.
PHP Solution:
<?php
$salary1 = 45000;
$salary2 = 38000;
$result = $salary1 > $salary2;
echo "Does Employee 1 earn more? ";
var_dump($result);
?>
Sample Output:
Does Employee 1 earn more? bool(true)
Explanation: The > operator checks whether the left-hand value is greater than the right-hand value. Since 45000 is greater than 38000, the result is true.
Concepts Covered: Greater-than operator, salary comparison.
Question 5: Check Whether One Number Is Less Than Another
Problem Statement: A shop has 15 laptops available and receives an order for 20 laptops. Check whether the available stock is less than the order quantity.
PHP Solution:
<?php
$availableStock = 15;
$orderQuantity = 20;
$result = $availableStock < $orderQuantity;
echo "Is stock less than the order quantity? ";
var_dump($result);
?>
Sample Output:
Is stock less than the order quantity? bool(true)
Explanation: The < operator checks whether the left-hand value is smaller than the right-hand value. Because 15 is less than 20, the result is true.
Concepts Covered: Less-than operator, inventory comparison.
Question 6: Check the Greater-Than-or-Equal Operator
Problem Statement: A student needs at least 40 marks to pass an examination. Check whether a student who scored exactly 40 marks meets the passing requirement.
PHP Solution:
<?php
$marks = 40;
$passingMarks = 40;
$result = $marks >= $passingMarks;
echo "Has the student passed? ";
var_dump($result);
?>
Sample Output:
Has the student passed? bool(true)
Explanation: The >= operator returns true when the left-hand value is greater than or equal to the right-hand value. A score of exactly 40 therefore meets the requirement.
Concepts Covered: Greater-than-or-equal operator, result checking.
Question 7: Check the Less-Than-or-Equal Operator
Problem Statement: A company allows employees to claim a travel allowance of up to ₹2,000. Check whether a claim of ₹1,800 is within the limit.
PHP Solution:
<?php
$travelClaim = 1800;
$maximumAllowance = 2000;
$result = $travelClaim <= $maximumAllowance;
echo "Is the claim within the limit? ";
var_dump($result);
?>
Sample Output:
Is the claim within the limit? bool(true)
Explanation: The <= operator returns true when a value is smaller than or equal to another value. Since ₹1,800 is within the ₹2,000 limit, the comparison returns true.
Concepts Covered: Less-than-or-equal operator, allowance validation.
Question 8: Compare Three Different Values
Problem Statement: A student scores 75 marks. Compare the score with 60 and 90 to determine whether it is greater than 60 and less than 90.
PHP Solution:
<?php
$marks = 75;
$aboveMinimum = $marks > 60;
$belowMaximum = $marks < 90;
echo "Above 60? ";
var_dump($aboveMinimum);
echo "Below 90? ";
var_dump($belowMaximum);
?>
Sample Output:
Above 60? bool(true)
Below 90? bool(true)
Explanation: The first comparison checks whether the marks are greater than 60. The second checks whether they are less than 90. Both comparisons return true for 75.
Concepts Covered: Multiple comparisons, Boolean expressions.
Question 9: Compare Values Using the Spaceship Operator
Problem Statement: Compare two product prices using PHP’s spaceship operator (<=>). Display the comparison result.
PHP Solution:
<?php
$price1 = 500;
$price2 = 700;
$result1 = $price1 <=> $price2;
$result2 = $price2 <=> $price1;
$result3 = $price1 <=> $price1;
echo "500 compared with 700: " . $result1 . "<br>";
echo "700 compared with 500: " . $result2 . "<br>";
echo "500 compared with 500: " . $result3;
?>
Sample Output:
500 compared with 700: -1
700 compared with 500: 1
500 compared with 500: 0
Explanation: The spaceship operator returns:
-1when the left-hand value is smaller.1when the left-hand value is greater.0when the values compare as equal.
It is useful for custom sorting and comparisons.
Concepts Covered: Spaceship operator, value ordering.
Question 10: Compare Employee Performance Scores
Problem Statement: An employee must score at least 80 in a performance assessment to qualify for a reward. Check whether an employee who scores 85 qualifies.
PHP Solution:
<?php
$employeeScore = 85;
$requiredScore = 80;
$qualified = $employeeScore >= $requiredScore;
echo "Employee Score: " . $employeeScore . "<br>";
echo "Required Score: " . $requiredScore . "<br>";
echo "Qualified for Reward: ";
var_dump($qualified);
?>
Sample Output:
Employee Score: 85
Required Score: 80
Qualified for Reward: bool(true)
Explanation: The >= operator checks whether the employee’s score meets or exceeds the required score. Since 85 is greater than 80, the employee qualifies.
Concepts Covered: Comparison operators, Boolean expressions, business rules.
Key Takeaways
==checks whether values are equal.!=and<>check whether values are not equal.===checks equality of both value and data type.!==checks whether values or their data types differ.>checks whether one value is greater than another.<checks whether one value is smaller than another.>=checks whether a value is greater than or equal to another.<=checks whether a value is less than or equal to another.<=>returns-1,0, or1based on the comparison.- Comparison expressions return Boolean results or, for
<=>, an integer result.
Frequently Asked Questions
1. What are comparison operators in PHP?
Comparison operators compare two values and determine their relationship, such as whether they are equal, different, greater, or smaller.
2. What is the difference between == and === in PHP?
The == operator compares values with applicable type conversion. The === operator checks both the value and data type without converting one operand to match the other.
3. What is the difference between != and !==?
The != operator checks whether values are unequal after applicable type conversion. The !== operator returns true when the values differ or their data types are different.
4. What does the >= operator mean?
The >= operator means greater than or equal to. For example, 50 >= 50 returns true.
5. What does the <= operator mean?
The <= operator means less than or equal to. For example, 20 <= 30 returns true.
6. What is the spaceship operator in PHP?
The <=> operator compares two values and returns -1, 0, or 1 depending on their ordering.
7. Do comparison operators return true or false?
Most comparison operators return a Boolean value, either true or false. The spaceship operator is an exception because it returns an integer comparison result.
8. Where are comparison operators used in PHP?
They are used in student result systems, salary comparisons, product price checks, form validation, loops, and conditional statements.
9. Can comparison operators compare strings?
Yes. PHP supports string comparisons. The result depends on the operator and the values being compared. For reliable text comparisons, consider the specific comparison rules needed by your application.
10. Why are comparison operators important?
They allow PHP programs to make decisions by checking whether conditions are satisfied, such as whether a student passed or a purchase meets a minimum amount.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
