Page 50 - Open Soource Technologies 304.indd
P. 50
Web Technologies-I
Notes strings are different lengths, the resulting string is the length of the shorter operand, and extra
trailing characters in the longer string are ignored. For example, “big drink” ^ “AA” is “# (“.
Left shift (<<)
The left shift operator shifts the bits in the binary representation of the left-hand operand left by
the number of places given in the right-hand operand. Both operands will be converted to integers
if they are not already. Shifting a binary number to the left inserts a 0 as the rightmost bit of the
number and moves all other bits to the left one place. For example, 3 << 1 (or binary 11 shifted
one place left) results in 6 (binary 110).
Note that each place to the left that a number is shifted results in a doubling of the number. The
result of left shifting is multiplying the left-hand operand by 2 to the power of the right-hand
operand.
Right shift (>>)
The right shift operator shifts the bits in the binary representation of the left-hand operand right
by the number of places given in the right-hand operand. Both operands will be converted to
integers if they are not already. Shifting a binary number to the right inserts a 0 as the leftmost
bit of the number and moves all other bits to the right one place. The rightmost bit is discarded.
For example, 13 >> 1 (or binary 1101) shifted one place right results in 6 (binary 110).
Logical Operators
Logical operators provide ways for you to build complex logical expressions. Logical operators
treat their operands as Boolean values and return a Boolean value. There are both punctuation
and English versions of the operators (|| and or are the same operator). The logical operators are:
Logical AND (&&, and)
The result of the logical AND operation is true if and only if both operands are true; otherwise, it
is false. If the value of the first operand is false, the logical AND operator knows that the resulting
value must also be false, so the right-hand operand is never evaluated. This process is called
short-circuiting, and a common PHP idiom uses it to ensure that a piece of code is evaluated only
if something is true. For example, you might connect to a database only if some flag is not false:
$result = $flag and mysql_connect( );
The && and and operators differ only in their precedence.
Logical OR (||, or)
The result of the logical OR operation is true if either operand is true; otherwise, the result is false.
Like the logical AND operator, the logical OR operator is short-circuited. If the left-hand operator
is true, the result of the operator must be true, so the right-hand operator is never evaluated. A
common PHP idiom uses this to trigger an error condition if something goes wrong. For example:
$result = fopen($filename) or exit( );
The || and or operators differ only in their precedence.
Logical XOR (xor)
The result of the logical XOR operation is true if either operand, but not both, is true; otherwise,
it is false.
Logical negation (!)
The logical negation operator returns the Boolean value true if the operand evaluates to false,
and false if the operand evaluates to true.
44 LOVELY PROFESSIONAL UNIVERSITY