Increment and compound assignment
In the previous lesson operators computed a new value, and the variables stayed as they were. Often you need something else: to change the variable itself, add to a score or append to a string.
Many calculators have a memory and an M+ key. It adds the number on the screen to what is already stored in memory, so you never type the old value again. In PHP this job is done by operators that change a variable in place.
Without them you have to write the variable's name twice:
<?php
$total = 10;
$total = $total + 5;
$total += 5;
$total *= 2;
echo $total, "\n"; // 40Let's go through it line by line:
$total = 10puts the number10into$total.$total = $total + 5reads the old value, adds5and writes15back.$total += 5does the same in a shorter way: now$totalholds20.$total *= 2multiplies by two, and the program prints40.
Compound assignment
A form like $total += 5 is called a compound assignment: the operator and the = sign are merged into one. Every arithmetic operator has one:
+=adds:$n += 2means$n = $n + 2;-=subtracts;*=multiplies;/=divides;%=keeps the remainder;**=raises to a power: after$c = 3; $c **= 2;the variable$cholds9.
The type of the result follows the same rules as plain arithmetic. After $n = 10; $n /= 4; the variable $n holds float(2.5), because 10 does not divide evenly by 4.
The .= operator for strings
The .= operator attaches a string to the right of the one already stored in the variable. That is how you build a string piece by piece:
$line = 'PHP';
$line .= ' ';
$line .= 8;
echo $line, "\n"; // PHP 8Before joining, the number 8 turns into the string '8', so $line ends up as PHP 8. This is where the M+ key stops being an exact picture: a calculator can only add numbers, while .= appends text.
Increment and decrement
Adding one comes up so often that there is a separate operator for it. $i++ increases $i by one; this is an increment. $i-- decreases it by one; this is a decrement.
Each of them has two forms. The difference shows when the result is stored in another variable or printed right away.
$i++: the old value first
Imagine a ticket machine in a queue. It hands you the number shown on the display and immediately switches the display to the next one. You hold the old number, and the display already shows the new one.
That is how $a = $i++ works: $a receives the old value of $i, and $i itself is one larger after the line.
++$i: add first
The ++$i form does the opposite: it switches the display first and then hands out the number from it. In $b = ++$i the variable $i is increased first, and $b receives the new value.
Click "Next step" and on every step compare two numbers: what $a or $b received and what $i holds after the line.
1<?php2 3$i = 1;4$a = $i++;5$b = ++$i;6$s = 'x';7$s .= $i;8echo $a, ' ', $b, ' ', $i, "\n";9echo $s, "\n";
The variable $i gets the value 1.
Both forms change the variable the same way: after $i++ and after ++$i, $i is one larger. The only difference is the value the expression itself hands back: the old one for $i++, the new one for ++$i.
The ticket machine is an imprecise picture here: its display only goes up, while $i-- and --$i decrease the variable by the same rules.
Interviewers often ask how $i++ differs from ++$i. The answer comes straight from this lesson:
- both operators increase
$iby one; $i++returns the value before the increase;++$ireturns the value after the increase.
Common mistake
A counter is incremented and the result is written straight back:
$count = 5;
$count = $count++; // ✗
echo $count, "\n"; // 5You expected 6, but PHP prints 5. $count++ hands back the old value 5 and raises $count to 6, but then = writes the handed-back five into $count. There is no error message. To increase the variable, one line $count++; without an assignment is enough.
Key points
$total += 5is short for$total = $total + 5;-=,*=,/=,%=and**=work the same way..=attaches a string to the right of the variable's value.$i++hands back the old value and++$ithe new one; the variable increases in both cases.$i = $i++leaves$iunchanged: the assignment writes back the old value.