Skip to content

PHP Tutorial Operators

Comparison Operators

Lesson 49 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

A comparison operator asks a question about two values and answers with true or false. You will use these in every if statement.

OperatorMeaningExample
==Equal (after type conversion)5 == "5" is true
===Identical (same value and type)5 === "5" is false
!= or <>Not equal5 != 6 is true
!==Not identical5 !== "5" is true
>Greater than7 > 3 is true
<Less than7 < 3 is false
>=Greater than or equal7 >= 7 is true
<=Less than or equal3 <= 2 is false
<=>Spaceship (compare and return -1, 0 or 1)3 <=> 7 is -1
index.php
PHP
<?php
var_dump(5 > 3);
var_dump(5 <= 3);
var_dump(5 == "5");
var_dump(5 === "5");
Output
OUTPUT
bool(true)
bool(false)
bool(true)
bool(false)

Comparing text

Strings are compared character by character. Capital letters come before small letters.

index.php
PHP
<?php
var_dump("apple" < "banana");
var_dump("Zebra" < "apple");
var_dump("hello" === "Hello");
Output
OUTPUT
bool(true)
bool(true)
bool(false)

The spaceship operator

<=> returns -1 if the left side is smaller, 0 if they are equal and 1 if the left side is bigger. It is the standard tool for sorting.

index.php
PHP
<?php
echo (3 <=> 7) . "\n";
echo (5 <=> 5) . "\n";
echo (9 <=> 2);
Output
OUTPUT
-1
0
1

Use it for sorting

index.php
PHP
<?php
$people = [
    ["name" => "Zoya", "age" => 30],
    ["name" => "Ali", "age" => 22],
    ["name" => "Mira", "age" => 27],
];

usort($people, fn($a, $b) => $a["age"] <=> $b["age"]);

foreach ($people as $p) {
    echo $p["name"] . " ";
}
Output
OUTPUT
Ali Mira Zoya
lightbulb
Prefer ===
Use === and !== by default. Reach for == only when you really want type conversion. The next lesson shows why.