Bug #70510 [Opn->Nab]: Variables defined by a cycle are not isolated and cause side effects

From: Date: Wed, 16 Sep 2015 17:56:49 +0000
Subject: Bug #70510 [Opn->Nab]: Variables defined by a cycle are not isolated and cause side effects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-196042@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70510&edit=1 ID: 70510 Updated by: requinix@php.net Reported by: mumu at seznam dot cz Summary: Variables defined by a cycle are not isolated and cause side effects -Status: Open +Status: Not a bug Type: Bug Package: Variables related Operating System: Windows PHP Version: 7.0.0RC2 Block user comment: N Private report: N New Comment: $item is still a reference to the last item after the first loop ends. When the second begins, it will overwrite $item with values from the array - dump out $list inside the loop and you'll see [c] change to 'xx' and 'yy'. unste($item) after the first loop to destroy the reference. Previous Comments: ------------------------------------------------------------------------ [2015-09-16 10:28:58] mumu at seznam dot cz Description: ------------ The first cycle has $item defined by reference. The second by value. The $item keeps being a reference to the (last item of the) array even it is used as by value inside the second cycle. As such, the two cycles are not properly isolated as would have been expected. Test script: --------------- <?php $list = array( 'a' => 'x', 'b' => 'y', 'c' => 'z', ); foreach ($list as &$item) { $item .= $item; } foreach ($list as $key => $item) { echo $key . ': ' . $item . "\n"; } ?> Expected result: ---------------- a: xx b: yy c: zz Actual result: -------------- a: xx b: yy c: yy ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70510&edit=1

« previous php.bugs (#196042) next »