Curriculum
In this tutorial, you will learn how to compare two values using PHP comparison operators.
A comparison operator compares two values and returns true if the comparison is correct and false if it is incorrect.
The comparison operators in PHP are illustrated in the table below:
Operator | Name | Description |
---|---|---|
== | Equal to | Return true if both operands are equal; otherwise, it returns false . |
!=, <> | Not equal to | Return true if both operands are equal; otherwise, it returns false . |
=== | Identical to | Return true if both operands have the same data type and equal; otherwise, it returns false . |
!== | Not identical to | Return true if both operands are not equal or not have the same data type; otherwise, it returns false . |
> | Greater than | Return true if the operand on the left is greater than the operand on the right; otherwise, it returns false . |
>= | Greater than or equal to | Return true if the operand on the left is greater than or equal to the operand on the right; otherwise, it returns false . |
< | Less than | Return true if the operand on the left is less than the operand on the right; otherwise, it returns false . |
<= | Less than or equal to | Return true if the operand on the left is less than or equal to the operand on the right; otherwise, it returns false . |
If both values are equal, the equality function returns true; otherwise, it returns false. Because 10 equals 10, the following example returns true:
<?php $x = 10; $y = 10; var_dump($x == $y); // bool(true)
Because 10 does not equal 20, the following example returns false:
<?php $x = 20; $y = 10; var_dump($x == $y); // bool(false)
When the number 20 is compared to the string ’20,’ it also returns true.
<?php $x = '20'; $y = 20; var_dump($x == $y); // bool(true)
If you want to compare two values while taking type into account, you can use the identical operator (===).
If the lefthand value is not equal to the righthand value, the not equal to (!=,>) operator returns true; otherwise, it returns false. As an example:
<?php $x = 20; $y = 10; var_dump($x != $y); // bool(true)
If both values are equal and of the same type, the identical operator returns true; otherwise, it returns false.
The identical operator is used to compare a string and a number in the following example. Because these values are of different types, it returns false:
<?php $x = '20'; $y = 20; var_dump($x === $y); // bool(false)
If the values are not equal or are not of the same type, the not identical operator (!==) returns true; otherwise, it returns false. As an example:
<?php $x = 20; $y = 10; var_dump($x != $y); // bool(true) $x = 20; $y = '20'; var_dump($x != $y); // bool(false)
If the lefthand value is greater than the righthand value, the greater than function returns true; otherwise, it returns false:
<?php $x = 10; $y = 20; var_dump($x > $y); // bool(false) var_dump($y > $x); // bool(true)
If the lefthand value is greater than or equal to the righthand value, the greater than or equal to operator returns true; otherwise, it returns false. As an example:
<?php $x = 20; $y = 20; var_dump($x >= $y); // bool(true) var_dump($y >= $x); // bool(true)
If the lefthand value is less than the righthand value, the less than operator returns true; otherwise, it returns false. As an example:
<?php $x = 20; $y = 10; var_dump($x < $y); // bool(false) var_dump($y < $x); // bool(true)
The less than or equal to operator returns true if the lefthand value is less than or equal to the righthand value; otherwise, it returns false. As an example:
<?php $x = 20; $y = 20; var_dump($x <= $y); // bool(true) var_dump($y <= $x); // bool(true)