Skip to content
Theory · Operators and comparison · lesson 3 of 8

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
<?php

$total = 10;
$total = $total + 5;
$total += 5;
$total *= 2;
echo $total, "\n"; // 40

Let's go through it line by line:

  1. $total = 10 puts the number 10 into $total.
  2. $total = $total + 5 reads the old value, adds 5 and writes 15 back.
  3. $total += 5 does the same in a shorter way: now $total holds 20.
  4. $total *= 2 multiplies by two, and the program prints 40.

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 += 2 means $n = $n + 2;
  • -= subtracts;
  • *= multiplies;
  • /= divides;
  • %= keeps the remainder;
  • **= raises to a power: after $c = 3; $c **= 2; the variable $c holds 9.

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:

PHP
$line = 'PHP';
$line .= ' ';
$line .= 8;
echo $line, "\n"; // PHP 8

Before 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.

LiveOld value and new valuestep 1 of 7 · frames: 1

The variable $i gets the value 1.

Compare what $a and $b received with what $i holds

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 $i by one;
  • $i++ returns the value before the increase;
  • ++$i returns the value after the increase.

Common mistake

A counter is incremented and the result is written straight back:

PHP
$count = 5;
$count = $count++; // ✗
echo $count, "\n"; // 5

You 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 += 5 is 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 ++$i the new one; the variable increases in both cases.
  • $i = $i++ leaves $i unchanged: the assignment writes back the old value.