php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41373 Incorrect expression evaluation with pre-increment
Submitted: 2007-05-12 08:33 UTC Modified: 2007-05-13 18:18 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: romy4 at online dot ua Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.2 OS: WinNT,Linux
Private report: No CVE-ID: None
 [2007-05-12 08:33 UTC] romy4 at online dot ua
Description:
------------
PHP incorrectly evaluates code with variable pre-increment

Reproduce code:
---------------
$i=5;
$i=++$i + ++$i;
echo $i;

Expected result:
----------------
Expecting 14
Evaluated 13



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-12 08:41 UTC] itonix at inet dot ua
Very! VERY  important bug
 [2007-05-12 09:38 UTC] johannes@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

The evaluation order of this construct is undefined and random things may happen.
 [2007-05-12 13:45 UTC] romy4 at online dot ua
sorry, check what? i thought '++' has higher priority than '+' that shown in the manual (http://www.php.net/manual/ru/language.operators.php) or not at last time?
 [2007-05-12 13:50 UTC] romy4 at online dot ua
i do not know how is php engine works inside but logically
++i should increment 'i' value by reference not copy of 'i' variable.
 [2007-05-13 15:00 UTC] carsten_sttgt at gmx dot de
> $i=++$i + ++$i;

There is one important rule: Don't change the value of a variable more then one times between sequence points.

For a better explanation you can start for example here [1]. It's the same reason as in bug #41364.

BTW:
Already with "$i = $i++;"  you have different results with different compilers (the way the code optimizer work).

Regards,
Carsten

[1] http://c-faq.com/expr/seqpoints.html
 [2007-05-13 18:18 UTC] derick@php.net
.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 18:01:34 2024 UTC