PHP Comparison Operators

Summary: in this tutorial, you will learn how to use PHP comparison operators to compare two values.

Introduction to PHP comparison operators #

A comparison operator allows you to compare two values and returns true if the comparison is truthful and false otherwise.

The following table shows the comparison operators in PHP:

OperatorNameDescription
==Equal toReturn true if both operands are equal; otherwise, it returns false.
!=, <>Not equal toReturn true if both operands are equal; otherwise, it returns false.
===Identical toReturn true if both operands have the same data type and are equal; otherwise, it returns false.
!==Not identical toReturn true if both operands are not equal or do not have the same data type; otherwise, it returns false.
>Greater thanReturn true if the operand on the left  is greater than the operand on the right; otherwise, it returns false.
>=Greater than or equal toReturn true if the operand on the left  is greater than or equal to the operand on the right; otherwise, it returns false.
<Less thanReturn true if the operand on the left is less than the operand on the right; otherwise, it returns false.
<=Less than or equal toReturn true if the operand on the left  is less than or equal to the operand on the right; otherwise, it returns false.

Equal To Operator (==) #

The equal to operator returns true if both values are equal; otherwise, it returns false.

The following example returns true because 10 is equal to 10:

<?php

$x = 10;
$y = 10;

var_dump($x == $y); // bool(true)

Try it

The following example returns false because 10 is not equal 20:


<?php

$x = 20;
$y = 10;
var_dump($x == $y); // bool(false)

Try it

The following example compares the number 20 with a string '20', it also returns true.

<?php

$x = '20';
$y = 20;
var_dump($x == $y); // bool(true)

Try it

If you want to compare two values with the consideration of type, you can use the identical operator (===).

Not equal to operator (!=, <>) #

The not equal to (!=, <>) operator returns true if the lefthand value is not equal to the righthand value; otherwise, it returns false. For example:

<?php

$x = 20;
$y = 10;

var_dump($x != $y); // bool(true)

Try it

Output:

bool(true)

Identical operator (===) #

The identical operator returns true if both values are equal and have the same type; otherwise returns false.

The following example uses the identical operator to compare a string and a number. It returns false because these values have different types:

<?php

$x = '20';
$y = 20;
var_dump($x === $y); // bool(false)

Try it

Not identical operator (!==) #

The not identical operator (!==) returns true if the values are not equal or they do not have the same type; otherwise, it returns false. For example:

<?php

$x = 20;
$y = 10;

var_dump($x != $y); // bool(true)

$x = 20;
$y = '20';
var_dump($x != $y); // bool(false)

Try it

Greater than (>) #

The greater-than operator returns true if the lefthand value is greater than the righthand value; otherwise, it returns false:

<?php

$x = 10;
$y = 20;

var_dump($x > $y); // bool(false)
var_dump($y > $x); // bool(true)

Try it

Greater than or equal to (>=) #

The greater than or equal to operator returns true if the lefthand value is greater than or equal to the righthand value; otherwise, it returns false. For example:

<?php

$x = 20;
$y = 20;

var_dump($x >= $y); // bool(true)
var_dump($y >= $x); // bool(true)

Try it

Less than (<) #

The less-than operator returns true if the lefthand value is less than the righthand value; otherwise, it returns false. For example:

<?php

$x = 20;
$y = 10;

var_dump($x < $y); // bool(false)
var_dump($y < $x); // bool(true)

Try it

Less than or equal to (<=) #

If the lefthand value is less than or equal to the righthand value, the less than or equal to operator returns true; otherwise, it returns false. For example:

<?php

$x = 20;
$y = 20;

var_dump($x <= $y); // bool(true)
var_dump($y <= $x); // bool(true)

Try it

In this tutorial, you have learned how to use the PHP comparison operators to compare two values of the same or different types.

Was this helpful?