Whacky increment/assignment logic with $foo++ vs ++$foo

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



<?PHP
$num = 123;
$num = $num++;
print $num;  //this prints 123 and not 124 ?!!

$num = 123;
$num = ++$num;
print $num; //this prints 124 as expected

$num = 123;
$num++;
print $num;  //this prints 124 as expected

$num = 123;
print $num++; //this prints 123 and not 124 ?!!
print $num++; //this NOW prints 124
?>

So then I read the manual because I think I'm loosing my mind and perhaps
it's backwards day and nobody told me:
http://us3.php.net/manual/en/language.operators.increment.php

I'm baffled as to the reasoning behind:
"$a++  :: Post-increment :: Returns $a, then increments $a by one."

Why would you EVER want $num = $num++; to give you back the value you
already had? Even if we did $foo = $bar++; I would still logically (and
common sensely) expect $foo to be the increment of $bar!

It also seems counter-intuitive, as I was always lead to believe everything
is processed on the right of an equals sign and then assigned back to the
left side of the equals sign. In this case, it does a mixture of both.

Can someone PLEASE explain why the developers of PHP chose this seemingly
whacky logic?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux