Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

< (^) Is Less Than



(^) Is Greater Than
<= (^) Is Less Than or Equal To
= (^) Is Greater Than or Equal To
== (^) Is Equal To
!= (^) Is Not Equal To
AND && (^) And
OR || (^) Or
XOR (^) Exclusive Or
! (^) Not
Notice that the equality operator is very similar to the assignment operator. That's
reasonable. One performs the action of making both sides equal; the right-side value is
copied to the variable on the left side. The other asks the question, "Are both sides
equal?" The inherent danger is that the two can be confused, and it is difficult to discover.
PHP will allow you to put an assignment inside the parentheses of an if statement. If you
have an if statement that always seems to evaluate one way, check to make sure you
haven't typed = when you meant ==.
If you are unfamiliar with logical operations, refer to Table 2.6. The first two columns
enumerate all the possible combined values of p and q, which stand for relational
expressions. The other four columns show the results of performing a logical operation
on p and q.
Table 2.6. Truth Table for Logical Operators
p q p AND q p OR q p XOR q !p
false false false false false true
false true false true true true
true false false true true false
true true true true false false
You might have noticed in Table 2.5 two versions of the logical operators. For instance,
there is both && and AND. Operationally, they are the same, but they differ in
precedence—a topic discussed at the end of this chapter. Aside from precedence, you are
free to use them interchangeably.
Bitwise Operators
A binary digit, which may be 1 or 0, is called a bit. Bitwise operators are similar to
logical operators, but where logical operators work on TRUE and FALSE, bitwise operators
view numbers from a binary perspective. When using logical operators, 1 and 10 are both
TRUE, but to a bitwise operator 1 looks like 0001 and 10 looks like 1010. A logical AND
of 1 and 10 results in TRUE. A bitwise AND of 1 and 10 results in 0. This is because each
bit of the two numbers is compared by a bitwise AND. Table 2.7 lists PHP's bitwise
operators.


Free download pdf