php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #60114 operator increment not working as expected
Submitted: 2011-10-22 16:46 UTC Modified: 2011-10-28 12:33 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: luuk34 at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.8 OS:
Private report: No CVE-ID: None
 [2011-10-22 16:46 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-22 17:14 UTC] luuk34 at gmail dot com
The actual result should even be:
111

and not:
011
 [2011-10-25 07:14 UTC] 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-25 18:53 UTC] luuk34 at gmail dot com
You mean to say the incremantation is done BEFORE the assignment? (in the case $foo=$foo++ )
 [2011-10-28 12:33 UTC] rquadling@php.net
-Status: Open +Status: Bogus
 [2011-10-28 12:33 UTC] rquadling@php.net
$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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 17:01:30 2024 UTC