php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31513 wrong expression execution order
Submitted: 2005-01-12 11:58 UTC Modified: 2005-01-12 13:30 UTC
From: dan at yes dot lt Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.0.3 OS: any
Private report: No CVE-ID: None
 [2005-01-12 11:58 UTC] dan at yes dot lt
Description:
------------
Arithmetic expression execution returns unexpected results with ++ or -- operators.

Reproduce code:
---------------
$a = 10;   
$c = $a++ + ++$a;
echo "$c\n";

$a = 10;
$c = ++$a + $a++;
echo "$c\n";

Expected result:
----------------
22
22

Actual result:
--------------
22
23 <- ?? Why ??

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-12 12:53 UTC] derick@php.net
You can never rely on this behavior, not in any language that I know about. Do not use the post/pre inc operation in assignments like this.
 [2005-01-12 13:17 UTC] dan at yes dot lt
<?php

$a = 10;  

$b = &$a;

$a = 10;  
$c = $a++ + ++$a; // Gauname 22, kaip ir turi buti
echo "$c\n\n";


$a = 10;
$c = ++$a + $a++; // Gauname 23, nors turetu buti 22
echo "$c\n\n";

?>
 [2005-01-12 13:18 UTC] dan at yes dot lt
it works if there is no $b =&$a reference, and fails, if there is that reference...
 [2005-01-12 13:30 UTC] derick@php.net
Please read me explanation again.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 13:01:33 2024 UTC