Skip to content

Instantly share code, notes, and snippets.

@lcherone
Last active December 13, 2017 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcherone/016a05de3dece10990d92fd912f98dd5 to your computer and use it in GitHub Desktop.
Save lcherone/016a05de3dece10990d92fd912f98dd5 to your computer and use it in GitHub Desktop.
Addition precedence post increment bug.
<?php
$a = 0; echo $a+$a+$a++; //0
$a = 1; echo $a+$a+$a++; //3
$a = 2; echo $a+$a+$a++; //6
$a = 3; echo $a+$a+$a++; //9
echo PHP_EOL;
$a = 0; echo $a+$a++; //1 <-- should be 0
$a = 1; echo $a+$a++; //3 <-- should be 2
$a = 2; echo $a+$a++; //5 <-- should be 4
$a = 3; echo $a+$a++; //7 <-- should be 6
echo PHP_EOL;
$a = 0; echo $a++; //0
$a = 1; echo $a++; //1
$a = 2; echo $a++; //2
$a = 3; echo $a++; //3
/*
0369
1357
0123
*/
/*
Which cause confusion for people who do..
echo $a+$a++; //3
echo $a+$a+$a++; //3
And ask themselves why:
https://stackoverflow.com/questions/47766470/why-these-both-post-increment-in-php-gives-the-same-answer
https://stackoverflow.com/questions/22314442/php-operator-precedence-undefined-order-of-evaluation
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment