PHP Comparison Operators (original) (raw)
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:
| 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 are equal; otherwise, it returns false. |
| !== | Not identical to | Return true if both operands are not equal or do 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. |
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)`Code language: PHP (php)
The following example returns false because 10 is not equal 20:
` <?php
$x = 20; $y = 10; var_dump($x == $y); // bool(false)`Code language: PHP (php)
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: PHP (php)
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: PHP (php)
Output:
bool(true)Code language: JavaScript (javascript)
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: PHP (php)
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)`Code language: PHP (php)
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)`Code language: PHP (php)
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: PHP (php)
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: PHP (php)
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: PHP (php)
In this tutorial, you have learned how to use the PHP comparison operators to compare two values of the same or different types.
Did you find this tutorial useful?