Comparison Operators
Comparison operators allow you to compare two values.
These operators test two values against each other, and if they relate correctly, the test returns true.
If the relationship specified by the test is incorrect, the return value is false. All comparison operators take two values for input.
See the following table:
Operator Name | Example | Task |
---|---|---|
Equal == | $a == $b | Return true if $a and $b are equal, if not equal then return false |
Identical === | $a === $b | Return true if $a and $b are equal and the same type, else return false |
Not equal != | $a != $b | Return true if $a and $b are not equal, if equal then return false |
Not equal <> | $a <> $b | Return true if $a and $b are not equal, if equal then return false |
Not identical !== | $a !== $b | Return true if $a and $b are unequal or different type, else return false |
Less than < | $a < $b | Return true if $a is less than $b |
Greater than > | $a > $b | Return true if $a is greater than $b |
Less than or equal to <= | $a <= $b | Return true if $a is less than or equal to $b |
Greater than or equal to >= | $a >= $b | Return true if $a is greater than or $b |
In above table, comparison operators determine whether the following conditions are present between two values:
Examples
Example 1: Comparing with equal operator ==
<?php $a = 'php'; $b = 'php'; var_dump($a == $b); //prints: bool(true) ?>
The above example displays the true
result as $a
and $b
are equal in values
Example 2: Comparing with identical operator ===
<?php $a = 'php'; $b = 'php'; var_dump($a === $b); //prints: bool(true) ?>
The above example displays the true
result as $a
and $b
are equal in values and also own the same type (string)
<?php $a = 1; //php integer $b = '1'; //php string var_dump($a === $b); //prints: bool(false) ?>
The above example displays the false
result because data type of $a
is string
and data type of $b
is integer
and we know identtical operator ===
checks types and values of given inputs
Example 4: Comparing with not equal operators !=
and <>
<?php $a = 'php'; $b = 'php'; var_dump($a != $b); //prints: bool(false) $a = 'php'; $b = 'php'; var_dump($a <> $b); //prints: bool(false) ?>
The above example displays the false
result because $a
and $b
are equal in values
Example 5: Comparing with not identical operator !==
<?php $a = 'php'; $b = 'php'; var_dump($a !== $b); //prints: bool(false) $c = 1; $d = '1'; var_dump($c !== $d); //prints: bool(true) ?>
In above example the first part returns false
result because $a
and $b
are equal in values and types,
and second part returns true
because $c
and $d
are not equal in types.
Example 6: Comparing with less than <
and greater than >
<?php $a = 'a'; $b = 'b'; var_dump($a < $b); //prints: bool(true) var_dump($b > $a); //prints: bool(true) $c = 1; $d = 2; var_dump($c < $d); //prints: bool(true) var_dump($d > $c); //prints: bool(true) ?>
Less than <
and greater than >
do not compare the length of given values or variables, actually these operators compare the sequence, in numerical values 1 is smaller than 2, 2 is smaller than 3... and alphabetically a
is smaller than b
. See the abobe example, it outputs the same.
Example 6: Comparing with less than or equal to <=
and greater than or equal to >=
<?php $a = 'a'; $b = 'bzd'; var_dump($a <= $b); //prints: bool(true) var_dump($b >= $a); //prints: bool(true) $a = 'a'; $b = 'a'; var_dump($a <= $b); //prints: bool(true) var_dump($b >= $a); //prints: bool(true) $c = 1; $d = 200; var_dump($c <= $d); //prints: bool(true) var_dump($d >= $c); //prints: bool(true) ?>
As we can see in above example, comapring 'a' <= 'b'
and 'a' <= 'a'
always return true.
var_dump()
function displays structured information i.e. type and value.