Few weeks ago I was in the middle of a debate with my girlfriend – which is also a programmer – about the increment unary operator. The main topic was what will the following code display:

$i = 1;
$i = $i++;
echo $i;

I said 1, she said 2. Of course, I was right in the end. But we were having this conversation while we were heading out to meet some friends, so, inevitably, others were drawn into the debate. One of which is Costin, who posted on his blog an entry called The trials and tribulations of the C incrementer on this subject. Long story short, if you post increment a variable, this operation will take place last and its result will be poped first from the stack and in some cases will be lost (our case – read Costin’s post for more details).

Other annoying examples using the increment operator:

$i = 1;
function someFunction( $_value ) {
    echo $_value;
}

someFunction( $i++ ); // will display 1
someFunction( ++$i ); // will display 2

$j = 1;
$j = ++$j;
echo $j; // will display 2

And again, the ZCE exam features this kind of questions.