Section 6.5. Comparison Operators


6.5. 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 $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


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 makes more sense.

The === (identical) operator is used very rarely compared to == (equality), but is useful nonetheless. Two variables are only identical if they hold the same value and if they are the same type, as demonstrated in this code example:

     print 12 ==  12;     print 12.0 ==  12;     print (0 + 12.0) ==  12;     print 12 + ===  12;     print "12" == 12;     print "12" === 12;

When you run that script using the CLI SAPI, you will find PHP outputs a 1 for the first 5 lines, and nothing for the last line. As mentioned already, PHP outputs a 1 for TRue, which means that the statements 12 equals 12, 12.0 equals 12, 0 + 12.0 equals 12, 12 is identical to 12, and "12" equals 12 are all true. However, nothing is output for the sixth line, which means that PHP considers the statement to be false, which is expected. Although "12" and 12 are the same value, they are not the same type; the former is a string, and the latter is an integer.

The === operator becomes important when you want to ensure PHP's type conversion isn't getting in the way of what you are trying to do. 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:

     if (0 === false) {             // this is true     }     if (0 === false) {             // this is false     }

The strpos( ) function returns the index at which it found one string inside another. If it finds a match at character 0, it returns 0; if it finds no match at all, it returns false. As a result, you should be careful to use === when checking the return value of strpos( ), so that you don't get confused between the two outcomes.



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net