Specifications

It is easy to confuse this with =, the assignment operator. This will work without giving an
error, but generally will not give you the result you wanted. In general, non-zero values evalu-
ate to true and zero values to false. Say that you have initialized two variables as follows:
$a = 5;
$b = 7;
If you then test $a = $b, the result will be true. Why? The value of $a = $b is the value
assigned to the left-hand side, which in this case is 7. This is a non-zero value, so the expres-
sion evaluates to true. If you intended to test $a == $b, which evaluates to false, you have
introduced a logic error in your code that can be extremely difficult to find. Always check your
use of these two operators, and check that you have used the one you intended to use.
This is an easy mistake to make, and you will probably make it many times in your program-
ming career.
Other Comparison Operators
PHP also supports a number of other comparison operators. A summary of all the comparison
operators is shown in Table 1.3.
One to note is the new identical operator, ===, introduced in PHP 4, which returns true only if
the two operands are both equal and of the same type.
TABLE 1.3 PHP’s Comparison Operators
Operator Name Use
== equals $a == $b
=== identical $a === $b
!= not equal $a != $b
<> not equal $a <> $b
< less than $a < $b
> greater than $a > $b
<= less than or equal to $a <= $b
>= greater than or equal to $a != $b
Logical Operators
The logical operators are used to combine the results of logical conditions. For example, we
might be interested in a case where the value of a variable, $a, is between 0 and 100. We
would need to test the conditions $a >= 0 and $a <= 100, using the AND operator, as follows
$a >= 0 && $a <=100
Using PHP
P
ART I
30
03 7842 CH01 3/6/01 3:39 PM Page 30