What are Comparison Operators in PHP?

Last updated on September 4, 2023

The comparison operator is mainly used in PHP to compare one variable value with another. These values can be strings or numbers. In PHP, comparison operators take simple values (numbers or strings) as arguments. The comparison operator gives us the result in Boolean. Let’s try to understand with the help of the table.

Here are the comparison operators along with their syntax and operations in PHP.

OperatorNameSyntaxOperation
==Equal to$x == $yReturns True if both the operands are equal
!=Not equal to$x != $yReturns True if both the operands are not equal
<>Not Equal To$x <> $yReturns True if both the operands are unequal
===Identical$x === $yReturns True if both the operands are equal and are of the same type
!==Not identical$x == $yReturns True if both the operands are unequal and are of different types
<Less than$x < $yReturns True if $x is less than $y
>Greater than$x > $yReturns True if $x is greater than $y
<=Less than or equal to$x <= $yReturns True if $x is less than or equal to $y
=>Greater Than or Equal To$x >= $yReturns True if $x is greater than or equal to $y
Comparison Operators List

Return values: The comparison operators returns bool (true or false).

Let’s try to understand with the following examples below:

1. Equality Operator (==)

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

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

2. Not equal to operator (!=)

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

$x = 20;
$y = 10;
var_dump($x != $y);
// output: bool(true)

3. Identical operator (===)

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

$x = "20";
$y = 20;
var_dump($x === $y); 
//Output: bool(false)

4. 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.

$x = 20;
$y = 10;
var_dump($x != $y);
//Output: bool(true)

$x = 20;
$y = "20";
var_dump($x != $y);
//Output: bool(false)

5. Greater than (>)

The greater than returns true if the left value is greater than the righthand value; otherwise, it returns false.

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

6. Greater than or equal to (>=)

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

$x = 20;
$y = 10;
var_dump($x >= $y); 
// output: bool(true)

7. Less than (<)

The less-than operator returns true if the left value is less than the righthand value; otherwise, it returns false.

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

8. Less than or equal to (<=)

If the left value is less than or equal to the righthand value, the less than or equal to the operator returns true; otherwise, it returns false.

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

Conclusion:

This article described Comparison Operators with examples. Also, we explained that how to use the PHP comparison operators to compare two values of the same or different types.


Written by
I am a web developer with over 2 years of industry experience, specializing in PHP, JavaScript, MySQLi, and Laravel. I create dynamic and user-friendly web applications, committed to delivering high-quality digital solutions.

Share on:

Related Posts

Improved by: Ayaz Ali Shah

Tags: PHP,