Bug #72173 [NEW]: very strange reference behavior (bug?) in PHP 5.6, 7.0

From: Date: Sat, 07 May 2016 14:16:54 +0000
Subject: Bug #72173 [NEW]: very strange reference behavior (bug?) in PHP 5.6, 7.0
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200935@lists.php.net to get a copy of this message
From: cf0hay at gmail dot com Operating system: Linux 64bit PHP version: 7.0.6 Package: Arrays related Bug Type: Bug Bug description:very strange reference behavior (bug?) in PHP 5.6, 7.0 Description: ------------ Affects both PHP 5.6.21 and PHP 7.0.6 . - Create an array with some elements - Iterate over this array by reference - assign (copy) this array by value to a different variable - Iterate over the previous array by reference again using the same variable - the copied array changes at the value which was the last of the original one Test script: --------------- <?php $x = [1,2,3]; foreach($x as &$ref){ $ref += 100; } /* $x === [101, 102, 103] at this point, $ref is bound to the last element of $x, that's OK */ $b = $x; /* this should logically copy $x to $b *by value* (COW happens) */ $b[0] = 0; /* this should force an array copy in memory */ /* here, $ref should be rebound to the elements of $x by reference */ foreach($x as &$ref){ $ref += 100; } /* at this point, $x === [201, 202, 203] which is correct, $ref refers to the last element of $x again, which is OK, BUT! As we copied $b by value before, is should stay as is, but the last element of $b changes to 203! */ var_dump($b); Expected result: ---------------- array(3) { [0]=> int(0) [1]=> int(102) [2]=> int(103) } Actual result: -------------- array(3) { [0]=> int(0) [1]=> int(102) [2]=> &int(203) } -- Edit bug report at https://bugs.php.net/bug.php?id=72173&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72173&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72173&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72173&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=72173&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=72173&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=72173&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=72173&r=needscript Try newer version: https://bugs.php.net/fix.php?id=72173&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=72173&r=support Expected behavior: https://bugs.php.net/fix.php?id=72173&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=72173&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=72173&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=72173&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72173&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=72173&r=dst IIS Stability: https://bugs.php.net/fix.php?id=72173&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=72173&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=72173&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=72173&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=72173&r=mysqlcfg

« previous php.bugs (#200935) next »