Current Location: Home> Latest Articles> PHP Comparison Operators Explained: Equal, Not Equal, Greater Than, Less Than, and Usage Guide

PHP Comparison Operators Explained: Equal, Not Equal, Greater Than, Less Than, and Usage Guide

M66 2025-10-26

Purpose of PHP Comparison Operators

In PHP, comparison operators are used to compare two variables or expressions to determine equality, size relationships, or type identity. They are essential for logical conditions and decision-making within code.

Main Comparison Operators in PHP

Below are the most common comparison operators in PHP and their explanations:

Equal (==)

Checks if two values are equal, ignoring their data types. Example:

$a = 5;
$b = '5';
var_dump($a == $b); // Outputs true, since only the values are compared

Not Equal (!=)

Checks whether two values are not equal, ignoring data types.

$a = 5;
$b = 8;
var_dump($a != $b); // Outputs true

Greater Than (>)

Returns true if the value on the left is greater than the one on the right.

$a = 10;
$b = 7;
var_dump($a > $b); // Outputs true

Less Than (<)

Returns true if the value on the left is less than the one on the right.

$a = 3;
$b = 8;
var_dump($a < $b); // Outputs true

Greater Than or Equal (>=)

Checks if the left value is greater than or equal to the right value.

$a = 5;
$b = 5;
var_dump($a >= $b); // Outputs true

Less Than or Equal (<=)

Checks if the left value is less than or equal to the right value.

$a = 4;
$b = 5;
var_dump($a <= $b); // Outputs true

Identical (===)

Compares both value and type. Returns true only when both match exactly.

$a = 5;
$b = '5';
var_dump($a === $b); // Outputs false because the types differ

Not Identical (!==)

Returns true when either the value or the type differs.

$a = 5;
$b = '5';
var_dump($a !== $b); // Outputs true

Summary

Comparison operators in PHP are essential for condition checking. Understanding their differences—especially between “equal (==)” and “identical (===)”—helps avoid logical errors and improves code clarity and reliability.