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

(singke) #1

All the assignment operators put a value into a variable. Specifically, they put a value on
the right side into a variable on the left side. You may not reverse the order. The
operators that combine another operator with an assignment operator operate on both the
right and left sides and then put the result in the variable on the left. Listing 2.11
demonstrates equivalent statements.


Table 2.10. Assignment Operators
Operator Operation Performed

= (^) Assign right side to left side
+= (^) Add right side to left side
-= (^) Subtract right side from left side
*= (^) Multiply left side by right side
/= (^) Divide left side by right side
%= (^) Set left side to left side modulo right side
&= (^) Set left side to bitwise AND of left side and right side
|= (^) Set left side to bitwise OR of left side and right side
^= (^) Set left side to bitwise XOR of left side and right side
.= (^) Set left side to concatenation of left side and right side
Expressions
Expressions are combinations of identifiers and operators. In most cases, they are the
familiar formulas you learned about in high school algebra. They are executed from left
to right; some operators are processed before others, and you can use parentheses to force
an operation to occur before the rest of the expression. But since the expression may be a
mix of different data types, you must be aware of how types are converted.
Listing 2.11 Using Assignment Operators
<?
// this assignment
$Count = $Count + 1;
// is the same as this assignment
$Count += 1;
?>
Two general rules are at work when an expression is evaluated. First, some operators
work only on certain data types. Second, if the operation is on a mix of an integer and a
double, the integer will be converted to a double.
Most operators work on numbers. If you attempt to add a string, the string will be
converted to a number. The contents of the string will determine whether it becomes an
integer or a double. PHP will make a good attempt at converting your string to a number.
It will strip leading whitespace and it will strip off all characters after a string of digits. It
will even read doubles with an exponent. But if PHP can't decide on a reasonable
numerical value, it will treat your string as zero.

Free download pdf