Bug #66089 [Com]: The result of post-increment/post-decrement operation is included in result
| From: | nikic@php.net | Date: | Wed, 13 Nov 2013 15:35:49 +0000 |
| Subject: | Bug #66089 [Com]: The result of post-increment/post-decrement operation is included in result | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-182740@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66089&edit=1
ID: 66089
Comment by: nikic@php.net
Reported by: alexanderhook at gmail dot com
Summary: The result of post-increment/post-decrement
operation is included in result
Status: Not a bug
Type: Bug
Package: Variables related
Operating System: Windows 7, Ubuntu 12
PHP Version: 5.4.21
Block user comment: N
Private report: N
New Comment:
@alexanderhook: Yes, this is exactly what Derick told you. For more information see http://us1.php.net/operators.precedence
(especially the last paragraph and example). Documentation for the technical reasons behind this can
be found at https://gist.github.com/nikic/6699370.
Previous Comments:
------------------------------------------------------------------------
[2013-11-13 14:46:44] alexanderhook at gmail dot com
Excuse me, please check again. The php versions showing different results for these statements:
In php 4.4.9 result is "2":
$i = 1;
echo ($i + ($i++));
In php 5.0.5 result is "2":
$i = 1;
echo ($i + ($i++));
In php 5.1.5 and higher the result is "3":
$i = 1;
echo ($i + ($i++));
------------------------------------------------------------------------
[2013-11-13 14:29:26] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Reading and writing the same variable during an assignment gives undefined behaviour.
------------------------------------------------------------------------
[2013-11-13 14:23:08] alexanderhook at gmail dot com
Test:
$i = 1;
$sum = $i + ($i++);
assert($sum == 2);
$i = 1;
$sum = $i + $i--;
assert($sum == 2);
------------------------------------------------------------------------
[2013-11-13 14:16:37] alexanderhook at gmail dot com
Description:
------------
In some cases, if variable is has participation in expression with post-increment, then the result
of post-increment operation is included in the result of the whole expression.
Test script:
---------------
$i = 1;
$sum = $i + ($i++); //3 , but should be 2
$i = 1;
$sum = 0 + $i + ($i++); //2 , correct
$i = 1;
$sum = $i + $i + ($i++); //3 , correct
$i = 1;
$sum = $i + $i--; // 1
$i = 1;
$sum = 0 + $i + $i--; // 2
$i = 1;
$sum = $i + $i + $i--; // 3
Expected result:
----------------
$i = 1;
$sum = $i + ($i++); //2
$i = 1;
$sum = 0 + $i + ($i++); //2
$i = 1;
$sum = $i + $i + ($i++); //3
$i = 1;
$sum = $i + $i--; // 2
$i = 1;
$sum = 0 + $i + $i--; // 2
$i = 1;
$sum = $i + $i + $i--; // 3
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66089&edit=1