Bug #70387 [Opn->Nab]: Array corruption after iteration with a &pointer
| From: | rasmus@php.net | Date: | Sun, 30 Aug 2015 23:45:16 +0000 |
| Subject: | Bug #70387 [Opn->Nab]: Array corruption after iteration with a &pointer | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-195639@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70387&edit=1
ID: 70387
Updated by: rasmus@php.net
Reported by: oleg dot nucer at gmail dot com
Summary: Array corruption after iteration with a &pointer
-Status: Open
+Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Windows 8.1 x64
PHP Version: 5.6.12
Block user comment: N
Private report: N
New Comment:
And this is not a bug. Think about what the code would look like if you unrolled your loops and
manually assigned each element to $s by reference and then started using $s as a non-reference
suddenly. Obviously the last thing $s is referencing will now be modified by that assignment unless
you clear it.
Previous Comments:
------------------------------------------------------------------------
[2015-08-30 23:24:52] mark at lange dot demon dot co dot uk
This is documented behaviour with an explanation on the [foreach PHP docs
page](http://php.net/manual/en/control-structures.foreach.php) and a description of how to avoid it
> Warning
> Reference of a $value and the last array element remain even after the foreach loop. It is
> recommended to destroy it by unset().
------------------------------------------------------------------------
[2015-08-29 11:39:03] oleg dot nucer at gmail dot com
Description:
------------
I've found a realy strange bug .. Just look to the script and to the output! I've killed a
half of our to understand what's going on in my script...
After iterating over array with a pointer variable the array becomes corrupted for next time
iteration...
Look at the last element in foreach loop, it's the same with previous!!
Test script:
---------------
<?
$sample = [
[ "first", 1 ],
[ "second", 2 ],
[ "third", 3 ]
];
foreach ( $sample AS &$s )
{
}
echo "Array: <pre>".print_r( $sample, 1 )."</pre><br/>";
foreach ( $sample AS $s )
{
echo "Element: <pre>".print_r( $s, 1 )."</pre>";
}
?>
Expected result:
----------------
Array:
Array
(
[0] => Array
(
[0] => first
[1] => 1
)
[1] => Array
(
[0] => second
[1] => 2
)
[2] => Array
(
[0] => third
[1] => 3
)
)
Element:
Array
(
[0] => first
[1] => 1
)
Element:
Array
(
[0] => second
[1] => 2
)
Element:
Array
(
[0] => third
[1] => 3
)
Actual result:
--------------
Array:
Array
(
[0] => Array
(
[0] => first
[1] => 1
)
[1] => Array
(
[0] => second
[1] => 2
)
[2] => Array
(
[0] => third
[1] => 3
)
)
Element:
Array
(
[0] => first
[1] => 1
)
Element:
Array
(
[0] => second
[1] => 2
)
Element:
Array
(
[0] => second
[1] => 2
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70387&edit=1