Doc #60114 [Opn->Bgs]: operator increment not working as expected

From: Date: Fri, 28 Oct 2011 12:33:41 +0000
Subject: Doc #60114 [Opn->Bgs]: operator increment not working as expected
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-7360@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=60114&edit=1 ID: 60114 Updated by: rquadling@php.net Reported by: luuk34 at gmail dot com Summary: operator increment not working as expected -Status: Open +Status: Bogus Type: Documentation Problem Package: *General Issues PHP Version: 5.3.8 Block user comment: N Private report: N New Comment: $foo = $foo++; and $foo = ($foo++); say, take the current value of $foo and treat that value as the result of the expression (in this instance the right hand side of the assignment). Then increment $foo. Then assign the value previously determined as the result to $foo, overriding the post-incremented value of $foo. The parenthesis do nothing here. As the documentation says in http://docs.php.net/manual/en/language.operators.increment.php, post-increment is "Returns $a, then increments $a by one." If you were to use ... <?php $foo=0; $newfoo=$foo++; echo $foo, $newfoo; $foo=0; $newfoo=($foo++); echo $foo, $newfoo; $foo=0; $foo++; echo $foo; ?> you would see the difference ... 10101 Previous Comments: ------------------------------------------------------------------------ [2011-10-25 18:53:58] luuk34 at gmail dot com You mean to say the incremantation is done BEFORE the assignment? (in the case $foo=$foo++ ) ------------------------------------------------------------------------ [2011-10-25 07:14:13] contact at jubianchi dot fr For me, it seems like this is the expected behavior : post-incrementation operator returns the value and the do the incrementation. So the first part of the snippet should print 0 as the second exemple. Only the third par of the snipper should print 1 as the $foo isn't being assigned the return value of the incrementation (we only increment the value). In the manual : $a++ Post-increment Returns $a, then increments $a by one. ------------------------------------------------------------------------ [2011-10-22 17:14:36] luuk34 at gmail dot com The actual result should even be: 111 and not: 011 ------------------------------------------------------------------------ [2011-10-22 16:46:24] luuk34 at gmail dot com Description: ------------ --- From manual page: http://www.php.net/language.operators.increment --- I think there's an error in the second example: $foo=($foo++); it should assign 0 to $foo, and after that increment $foo with 1. But the value of $foo after this line of code is still 0. Test script: --------------- <?php $foo=0; $foo=$foo++; print $foo; $foo=0; $foo=($foo++); print $foo; $foo=0; $foo++; print $foo; ?> Expected result: ---------------- 011 Actual result: -------------- 001 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=60114&edit=1

« previous php.doc.bugs (#7360) next »