php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40755 Increment/Decrement operator calculation oddity
Submitted: 2007-03-08 01:43 UTC Modified: 2007-03-08 08:39 UTC
From: henson dot garth at gmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.1 OS: Linux
Private report: No CVE-ID: None
 [2007-03-08 01:43 UTC] henson dot garth at gmail dot com
Description:
------------
The increment/decrement operators seem to calculate out of order compared to other languages and even previous versions of PHP. I compared this report numerous times against a 4.3.11 install that I am running to verify the inconsistency. In the case of PHP 5.2.x, the increment operator seems to be processed in an inappropriate order to the rest of the statement. In 4.3.11, everything is presented just as expected (as my Expected results display below).

Reproduce code:
---------------
$a = array(); // Begin Test 1
$b = 0;
$a[$b++] = $b;
var_dump($a);

$a = array(); // Begin Test 2
$b = 0;
$a[$b] = $b++;
var_dump($a);

$a = array(); // Begin Test 3
$b = 10;
$a[$b--] = $b;
var_dump($a);

$a = array(); // Begin Test 4
$b = 10;
$a[$b] = $b--;
var_dump($a);

Expected result:
----------------
array(1){[0] => int(1)}
array(1){[0] => int(0)}
array(1){[10] => int(9)}
array(1){[10] => int(10)}

Actual result:
--------------
array(1){[0] => int(1)}
array(1){[1] => int(0)}
array(1){[10] => int(9)}
array(1){[9] => int(10)}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-08 08:39 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

Using and modifying the same variable when using post/pre- decrement/increment is undefined (and not only in PHP).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 03:01:29 2024 UTC