Comparison Operators
Comparison operators return either true or false, and thus are suitable for use in conditions. PHP has several to choose from, and they are listed in Table 6-5.
Table 6-5. The comparison operators
|
== |
Equals |
True if |
|
=== |
Identical |
True if |
|
!= |
Not equal |
True if |
|
<> |
Not equal |
True if |
|
!= = |
Not identical |
True if |
|
< |
Less than |
True if |
|
> |
Greater than |
True if |
|
<= |
Less than or equal |
True if |
|
>= |
Greater than or equal |
True if |
Comparison operators such as <, >, and = = return true or false depending on the result of the comparison, and it is this value that PHP uses to decide actions. For example:
if ($foo < 10) {
// do stuff
}The less-than operator, <, will compare $foo to 10, and if it is less than (but not equal to) 10, then < will return true. This will make the line read if (true) {. Naturally, true is always true, so the true block of the if statement will execute.
PHP programmers prefer != to <>, despite them doing the same thing. This bias is because PHP's syntax is based on C, which uses != exclusively, and it is worth holding on to. For example, 9 <> "walrus" is true, but not because 9 is either greater or less than "walrus" as the notation <> suggests. In this example, != just ...