By Richard Blum

There are a few different shortcuts you can use when implementing arithmetic operators in your PHP code. A common function in programming code is to perform a mathematical operation on a value stored in a variable and then store the result back in the same variable, like this:

$counter = $counter + 1;

This code adds 1 to the value currently stored in the $counter variable and then saves the result back in the $counter variable. PHP provides a handy shortcut method for doing this:

$counter += 1;

This code accomplishes the exact same thing, but in a shorter form. You can use the same shortcut with any type of arithmetic operator:

$total *= 1.10;

This example multiplies the value stored in the $total variable by 1.10 and stores the result back in the $total variable. You can also use variables on the right side of the assignment operation:

$total *= $taxrate;

This is the same as typing the following:

$total = $total * $taxrate;

That can really save some typing for you!

Two other types of arithmetic shortcuts are the incrementor and decrementor operators. The incrementor operator adds 1 to a variable’s value:


The decrementor operator subtracts 1 from the variable’s value:


Now that’s really saving some typing!

The arithmetic shortcut operators assume there’s already a value stored in the variable before the operation. If there isn’t, PHP will generate a warning message, telling you that it assumes the initial value is 0. It’s always a good idea to initialize a variable to a known value before trying to use it in any arithmetic operations.