PHP Relational Operators
In this page:
$a == $b; // equal (after type conversion)
$a === $b; // equal and same type
$a != $b; // not equal
$a !== $b; // not identical
$a > $b; $a < $b; $a >= $b; $a <= $b;
Equality and Inequality
== checks whether two values are equal *after* PHP converts them to a common type if needed, so "5" == 5 evaluates to true even though one side is a string.
!= is simply its opposite, flagging values as different once that same type coercion has been applied.
Example: Equality and Inequality
<?php
// Print a detailed dump (with types) of `"5" == 5`
var_dump("5" == 5);
// Print a detailed dump (with types) of `5 != 6`
var_dump(5 != 6);
?>
Login to try C/C++/Java/PHP code in the editor
Strict Equality and Inequality
=== requires both the value *and* the type to match exactly, so "5" === 5 is false — a string can never strictly equal an integer, no matter what it looks like. !== is its inverse.
Reaching for the strict versions by default avoids a whole category of subtle type-coercion bugs.
Example: Strict Equality and Inequality
<?php
// Print a detailed dump (with types) of `"5" === 5`
var_dump("5" === 5);
// Print a detailed dump (with types) of `"5" !== 5`
var_dump("5" !== 5);
?>
Login to try C/C++/Java/PHP code in the editor
Greater Than and Less Than
> and < compare two values by ordinary numeric or string ordering, returning true only when the comparison is strictly one-sided — equal values fail both > and <, which is exactly why the equal-or-greater variants below exist for cases where a tie should count.
Example: Greater Than and Less Than
<?php
// Print a detailed dump (with types) of `5 > 3`
var_dump(5 > 3);
// Print a detailed dump (with types) of `5 < 5`
var_dump(5 < 5);
?>
Login to try C/C++/Java/PHP code in the editor
Greater Than or Equal to and Less Than or Equal to
>= and <= extend > and < to also accept equality, which is what you want for boundary checks like 'is this age old enough' or 'has this value reached its limit' — situations where the boundary value itself should pass the check, not just values strictly beyond it.
Example: Greater Than or Equal to and Less Than or Equal to
<?php
// Declare `$age`, set to `18`
$age = 18;
// Print a detailed dump (with types) of `$age >= 18`
var_dump($age >= 18);
// Print a detailed dump (with types) of `$age <= 17`
var_dump($age <= 17);
?>
Login to try C/C++/Java/PHP code in the editor
Spaceship Operator (<=>)
<=>, the spaceship operator, condenses a three-way comparison into one expression: it returns -1, 0, or 1 depending on whether the left side is smaller, equal, or larger.
It exists specifically to simplify writing custom sort comparison functions, where you'd otherwise need three separate if branches.
Example: Spaceship Operator (<=>)
<?php
// Print `3 <=> 5, "\n"` to the output
echo 3 <=> 5, "\n";
// Print `5 <=> 5, "\n"` to the output
echo 5 <=> 5, "\n";
// Print `8 <=> 5` to the output
echo 8 <=> 5;
?>
Login to try C/C++/Java/PHP code in the editor
- Using
==when===is needed, so"0" == falseor"abc" == 0(before PHP 8) give surprising results. - Writing
=instead of==in a comparison such asif ($x = 5), which assigns 5 and is always true. - Assuming the
<=>result is boolean, when it returns-1,0or1.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: