Specifications

Combination Assignment Operators
In addition to the simple assignment, there is a set of combined assignment operators. Each of
these is a shorthand way of doing another operation on a variable and assigning the result back
to that variable. For example
$a += 5;
This is equivalent to writing
$a = $a + 5;
Combined assignment operators exist for each of the arithmetic operators and for the string
concatenation operator.
A summary of all the combined assignment operators and their effects is shown in Table 1.2.
TABLE 1.2 PHP’s Combined Assignment Operators
Operator Use Equivalent to
+= $a += $b $a = $a + $b
-= $a -= $b $a = $a - $b
*= $a *= $b $a = $a * $b
/= $a /= $b $a = $a / $b
%= $a %= $b $a = $a % $b
.= $a .= $b $a = $a . $b
Pre- and Post-Increment and Decrement
The pre- and post- increment (++) and decrement (--) operators are similar to the += and -=
operators, but with a couple of twists.
All the increment operators have two effectsthey increment and assign a value. Consider the
following:
$a=4;
echo ++$a;
The second line uses the pre-increment operator, so called because the ++ appears before the
$a. This has the effect of first, incrementing $a by 1, and second, returning the incremented
value. In this case, $a is incremented to 5 and then the value 5 is returned and printed. The
value of this whole expression is 5. (Notice that the actual value stored in $a is changed: We
are not just returning $a + 1.)
Using PHP
P
ART I
28
03 7842 CH01 3/6/01 3:39 PM Page 28