← Back to PHP Course | Chapter 3: Operators | Lesson 2 of 8

PHP Relational Operators

Relational operators ask questions that have a yes-or-no answer, such as 'is this bigger than that?' or 'are these two the same?'. It is like a referee comparing two runners.
Syntax
php
$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
<?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);
?>

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
<?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);
?>

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
<?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);
?>

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
<?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);
?>

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
<?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;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using == when === is needed, so "0" == false or "abc" == 0 (before PHP 8) give surprising results.
  2. Writing = instead of == in a comparison such as if ($x = 5), which assigns 5 and is always true.
  3. Assuming the <=> result is boolean, when it returns -1, 0 or 1.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.