| Table 1
The Comparison Operators in PHP |
| ==, != |
Do
NOT
forget it is == not the single
= (or assignment operator). If your ifs are giving you
problems - look for a single = as source of trouble. Also see note below
on how equality is tested for . |
| >, >= |
greater than, greater than or equal
to comparison operations |
| <, <= |
if less than, less than or equal
to comparison operations |
| ===, !== |
identical operators, the arguments
must be of the same type as well as being equal to each other -
thus 0 !== "0" is true |
|
The Boolean or Logical Operators |
| expr1
|| expr2, expr1 or expr2 |
Is
true if either(or both) of the expressions are true.
Note or has lower precedence than
|| and thus:
$i = $i+$n or $i < 1 is equivalent
to ($i = $i+$n) || $i < 1 |
| expr1
xor expr2
|
Is true
only if one of the expressions are true; false if both are true or
both are false
|
| expr1
&& expr2, expr1 and expr2 |
Is true
if and only if both of the expressions are true.
Note and has lower precedence than
&& and thus:
$i = $i+$n and $i < 1 is equivalent
to ($i = $i+$n) && $i < 1 |
!
expr1 |
The
not operator reverses expr1 logic state, true becomes false and
false becomes true |
Strings can be compared using the logic operators directly;
but there is a danger . Because PHP tries to
convert strings to numbers, even when two strings are involved in the comparison,
ambiguous results can be returned. To be safe, use strcmp() and related
string comparison functions for best results.
Also note the difference between the "Not" operators: !
is
used for logical operations; ~ is
used for bitwise operations |
|
The Bitwise Operators |
| expr1
& expr2
=> and operator |
Only
bits that are set(equal to 1)in both expressions are set. Thus:
4 & 12 = 4 or in bit mode: 0100 & 1100 = 0100 |
| expr1
| expr2
=> or operator |
Bits
that are set in either expression are set; otherwise 0.
Thus: 4 | 12 = 12 or in bit mode: 0100 | 1100 = 1100 |
| expr1
^ expr2 => xor operator |
Bits
that are set in expr1 or expr2 but not both are set.
Thus: 4 ^ 12 = 8 or in bit mode: 0100 ^ 1100 = 1000 |
| ~expr1
=> not operator
|
Bits
are reversed. Thus: ~12 = 3 or in bitmode: ~1100 = 0011 |
| expr1
<< n => left shift operator |
expr1's
bit are shifted n positions to the left; this is equivalent to expr1
x 2**n. Thus: 3 << 2 = 12 or in bit mode 0011 << 2 = 1100 |
| expr1
>> n => right shift operator |
expr1's
bit are shifted n positions to the right; this is equivalent to expr1
/ 2**n. Thus: 3 >> 2 = 0 or in bit mode 0011 >> 2 = 0000 |
|
The Control of Flow Commands |
| if (test) statement;
if (test){ ..statements; }
if (test) statement else statement;
if (test) statement; [elseif(test) statement;] [ else statement;]
if (test){ ..statements;} [elseif(test){
..statements;} ] [else {..statements;}] |
if statements in PHP are
full powered.
Developers can use a string of elseif conditional
continuations; they can use brackets, {..statements;}, to contain
blocks of statements or they can use the simple one statement
conditional. |
| ternary operator:
(test)? expression : expression2; |
Some people like it; others don't.
$color = ($cnt++ % 2)? "aqua" : "green";
|
| switch (expression){
case value-1: case alternate-value:
[..state |
We have highlighted case
alternate-value: for 2 reasons. 1) |
Like most programming languages, PHP supports both logical and bitwise operations.
In effect the bitwise operations safely applied to integer variables - their effect
on booleans, float/double or string variables can be predicted but are not really
useful.
Note that the switch statement allows for strings to be used as case constants.
However, PHP falls short of the flexibility provided in the equivalent select
statement used in VB or VBScript. But PHP does provide the ternary operator
which can be useful in simplifying complex logic expressions. In general PHP
is on par with modern languages like Java and C# for it logic and bitwise operations.