++ Riddle

<?php

$a = 5;
$b = $a++-++$a;

echo $b; // -2
echo $a; // 7

// This, sadly, doesn't compile
//$b = $a+++++$a;

$a = 5;
$b = $a++ + ++$a; // 12
echo $b; // 12
echo $a; // 7

PHP has no operator ++-++. This has to be read with spaces, as ++ - ++: there is one post increment operator, a subtraction then a pre-increment operator.

In this case, the first operator increment $a from 5 to 6, but returns the original 5. The next operator is the subtraction, which works on the result of the pre-increment operator: this one turns the 6 to 7. The result is then -2.

PHP cannot compile +++++, as it confuses addition and increment operators. Adding spaces makes the code valid, and the same order of operation applies as above, leading to 12.

The original post dates from 2011, and was tested in different languages: the result may surprise you. At least, PHP has been consistent with this since then.

See Also

PHP Features

Last updated: 13 August 2026