php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66089 The result of post-increment/post-decrement operation is included in result
Submitted: 2013-11-13 14:16 UTC Modified: 2013-11-13 14:46 UTC
From: alexanderhook at gmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.4.21 OS: Windows 7, Ubuntu 12
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: alexanderhook at gmail dot com
New email:
PHP Version: OS:

 

 [2013-11-13 14:16 UTC] 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



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-11-13 14:23 UTC] alexanderhook at gmail dot com
-Operating System: Windows, Ubuntu +Operating System: Windows 7, Ubuntu 12
 [2013-11-13 14:23 UTC] 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:29 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2013-11-13 14:29 UTC] 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:46 UTC] 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 15:35 UTC] nikic@php.net
@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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jan 14 15:01:33 2025 UTC