PHP Comparison Operators
Description
Comparison operators return either true or false, and thus are suitable for use in conditions.
PHP Comparison Operators
Comparison operators in PHP has are listed in the following table.
Operator | Name | Description |
---|---|---|
== | Equals | True if $a is equal to $b |
=== | Identical | True if $a is equal to $b and of the same type |
!= | Not equal | True if $a is not equal to $b |
<> | Not equal | True if $a is not equal to $b |
!= = | Not identical | True if $a is not equal to $b or if they are not of the same type |
< | Less than | True if $a is less than $b |
> | Greater than | True if $a is greater than $b |
<= | Less than or equal | True if $a is less than or equal to $b |
>= | Greater than or equal True if $a is greater than or equal to $b |
The === (identical) says two variables are only identical if they hold the same value and if they are the same type, as demonstrated in this code example:
<?PHP/*from w ww . ja va 2 s . c om*/
print 12 == 12;
print 12.0 == 12;
print (0 + 12.0) == 12;
print 12 === 12;
print "12" == 12;
print "12" === 12;
?>
The code above generates the following result.
Example Identical Operator
The === operator is useful when comparing against false with 0 and empty string.
For example, PHP considers an empty string (""), 0, and false to be equal when used with ==, but using === allows you to make the distinction. For example:
<?PHP//from ww w. ja v a 2 s .com
if (0 === true) {
print(" this is true ");
}
if (0 === false) {
print("this is false ");
}
?>