Introduction:
Learn the ins and outs of PHP comparison operators with our comprehensive guide. Whether you’re a beginner or an experienced developer, understanding how to compare values in PHP is fundamental to writing efficient and error-free code. This lesson covers the various comparison operators, their usage, and provides practical examples to solidify your knowledge.
Here are the most common PHP comparison operators:
Checks if two values are equal.
$a == $b; // true if $a is equal to $b
Checks if two values are equal and of the same data type.
$a === $b; // true if $a is equal to $b and they are of the same type
Checks if two values are not equal.
$a != $b; // true if $a is not equal to $b
Checks if two values are not equal or not of the same data type.
$a !== $b; // true if $a is not equal to $b or they are not of the same type
Checks if the value on the left is greater than the value on the right.
$a > $b; // true if $a is greater than $b
Checks if the value on the left is less than the value on the right.
$a < $b; // true if $a is less than $b
Checks if the value on the left is greater than or equal to the value on the right.
$a >= $b; // true if $a is greater than or equal to $b
Checks if the value on the left is less than or equal to the value on the right.
$a <= $b; // true if $a is less than or equal to $b
Here are some examples:
$a = 10;
$b = 5;
// Equal
if ($a == $b) {
echo "$a is equal to $b";
}
// Identical
if ($a === $b) {
echo "$a is identical to $b";
}
// Not Equal
if ($a != $b) {
echo "$a is not equal to $b";
}
// Not Identical
if ($a !== $b) {
echo "$a is not identical to $b";
}
// Greater Than
if ($a > $b) {
echo "$a is greater than $b";
}
// Less Than
if ($a < $b) {
echo "$a is less than $b";
}
// Greater Than or Equal To
if ($a >= $b) {
echo "$a is greater than or equal to $b";
}
// Less Than or Equal To
if ($a <= $b) {
echo "$a is less than or equal to $b";
}
Keep in mind that when comparing values of different types using the equality (==) operator, PHP will perform type coercion.
If you want to compare values without type coercion, use the identical (===) or not identical (!==) operators.
Complete example in html with explanation
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Comparison Example</title>
</head>
<body>
<h2>PHP Comparison Example</h2>
<!-- HTML form to input two numbers -->
<form method="post" action="">
<label for="number1">Enter the first number:</label>
<input type="text" name="number1" required>
<label for="number2">Enter the second number:</label>
<input type="text" name="number2" required>
<button type="submit">Compare Numbers</button>
</form>
<?php
// PHP code to compare the entered numbers
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the input values from the form
$number1 = $_POST["number1"];
$number2 = $_POST["number2"];
// Perform comparisons
if ($number1 == $number2) {
$result = "The numbers are equal.";
} elseif ($number1 < $number2) {
$result = "The first number is less than the second number.";
} else {
$result = "The first number is greater than the second number.";
}
// Display the result
echo "<p>Comparison Result: $result</p>";
}
?>
</body>
</html>
Explanation:
Finally, the PHP code displays the result within a paragraph (<p>) tag in the HTML document.
This example demonstrates a basic use of PHP comparison operators within an HTML form.

<?php
session_start();
// Quiz questions and answers
$questions = array(
"1" => array(
"question" => "What does the PHP comparison operator === check for?",
"options" => array(
"a" => "Equality",
"b" => "Identity (both value and type)",
"c" => "Inequality",
"d" => "Greater than or equal to"
),
"correct_answer" => "b"
),
"2" => array(
"question" => "Which comparison operator is used to check if two values are not equal and of the same data type?",
"options" => array(
"a" => "!=",
"b" => "<>",
"c" => "!==",
"d" => "=="
),
"correct_answer" => "c"
),
// Add more questions here...
);
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Process the submitted answers
$_answers = $_POST['answers'];
$score = 0;
foreach ($_answers as $question_id => $selected_option) {
if ($selected_option === $questions[$question_id]["correct_answer"]) {
$score++;
}
}
// Save the score in the session
$_SESSION['quiz_score'] = $score;
// Redirect to the results page
header("Location: results.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Quiz Application</title>
</head>
<body>
<h2>PHP Quiz Application</h2>
<form method="post" action="">
<?php foreach ($questions as $id => $data): ?>
<p><?= $data['question']; ?></p>
<?php foreach ($data['options'] as $option_id => $option): ?>
<label>
<input type="radio" name="answers[<?= $id; ?>]" value="<?= $option_id; ?>" required>
<?= $option; ?>
</label><br>
<?php endforeach; ?>
<?php endforeach; ?>
<button type="submit">Submit Answers</button>
</form>
</body>
</html>
results.php:
<?php
session_start();
// Check if the has completed the quiz
if (!isset($_SESSION['quiz_score'])) {
header("Location: index.php");
exit();
}
// Display the quiz results
$score = $_SESSION['quiz_score'];
session_unset(); // Clear the session data
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Quiz Results</title>
</head>
<body>
<h2>PHP Quiz Results</h2>
<p>Your score: <?= $score; ?> out of <?= count($questions); ?></p>
</body>
</html>
This is a basic example, and you can customize and extend it based on your specific requirements. Add more questions to the $questions array in index.php to create a longer quiz.
Here’s a quiz to test your understanding of PHP comparison operators. Each question has multiple-choice answers, and the correct answers are provided at the end.
a) Equality
b) Identity (both value and type)
c) Inequality
d) Greater than or equal to
a) !=
b) <>
c) !==
d) ==
a) Not equal
b) Greater than or equal to
c) Not identical (both value and type)
d) Identical (both value and type)
a) true
b) false
c) undefined
d) null
a) <=
b) >=
c) ==
d) <>
a) true
b) false
c) undefined
d) null
a) Equal
b) Not equal
c) Greater than or equal to
d) Identical (both value and type)
a) <=
b) <
c) ==
d) >
a) true
b) false
c) undefined
d) null
a) >=
b) <=
c) ==
d) <>
Answers:
1-b) Identity (both value and type)
2-c) !==
3-c) Not identical (both value and type)
4-a) true
5-a) <=
a) true
6-b) Not equal
7-b) <
8-a) true
9-a) >=