PHP Comparison Operators

Created with Sketch.

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 illustrates 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 equal; otherwise, it returns false.
!==Not identical toReturn true if both operands are not equal or 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.

Equality Operator (==)

The equality returns true if both values are equal; otherwise, it returns false. The following example returns true because 10 is equal 10:

<?php

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

Code language: HTML, XML (xml)

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


<?php

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

Code language: HTML, XML (xml)

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)

Code language: HTML, XML (xml)

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)

Code language: HTML, XML (xml)

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)

Code language: HTML, XML (xml)

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 return false. For example:

<?php

$x = 20;
$y = 10;

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

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

Code language: HTML, XML (xml)

Greater than (>)

The greater than return 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)

Code language: HTML, XML (xml)

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)

Code language: HTML, XML (xml)

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)

Code language: HTML, XML (xml)

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)

Code language: HTML, XML (xml)

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

Leave a Reply

Your email address will not be published. Required fields are marked *