Comparison operators allow you to compare two values. They return a Boolean value (either true or false) based on whether the operands match the condition.
If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers.
Operator | Description | Example |
---|---|---|
== | Equal | $a==$b returns true if $a is equal to $b. |
=== | Identical | $a===$b returns true if $a is equal to $b, and they are of the same type. |
!= | Not Equal | $a!=$b returns true if $a is not equal to $b. |
<> | Not Equal | $a<>$b returns true if $a is not equal to $b. |
!== | Not Identical | $a!==$b returns true if $a is not equal to $b, or they are not of the same type. |
< | Less than | $a<$b returns true if $a is strictly less than $b. |
> | Greater than | $a>$b returns true if $a is strictly greater than $b. |
<= | Less than or equal to | $a<=$b returns true if $a is less than or equal to $b. |
>= | Greater than or equal to | $a>=$b returns true if $a is greater than or equal to $b. |
<html> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $a = 1; $b = 2; $c = 2; echo "a = ", $a,"<br />b = ",$b, "<br />c = ", $c, "<br />"; echo "The value of a==b is: ", $a==$b, "<br />"; echo "The value of a===b is: ", $a===$b, "<br />"; echo "The value of b===c is: ", $b==$c, "<br />"; echo "The value of a!=b is: ", $a!=$b, "<br />"; echo "The value of a<>b is: ", $a<>$b, "<br />"; echo "The value of b==c is: ", $b==$c, "<br />"; echo "The value of a!==b is: ", $a!==$b, "<br />"; echo "The value of b!==c is: ", $b!==$c, "<br />"; echo "The value of a<b is: ", $a<$b, "<br />"; echo "The value of a>b is: ", $a>$b, "<br />"; echo "The value of a<=b is: ", $a<=$b, "<br />"; echo "The value of a>=b is: ", $a>=$b, "<br />"; echo "The value of b<c is: ", $b<$c, "<br />"; echo "The value of b>c is: ", $b>$c, "<br />"; echo "The value of b<=c is: ", $b<=$c, "<br />"; echo "The value of b>=c is: ", $b>=$c, "<br />"; ?> </p> </body> </html> |