php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #37410 foreach by reference exists outside of scope
Submitted: 2006-05-11 18:26 UTC Modified: 2007-08-17 22:03 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: php at kormoc dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
 [2006-05-11 18:26 UTC] php at kormoc dot com
Description:
------------
Foreach by reference does not honour scope.

Basically, this was already reported at the following two bug reports:

http://bugs.php.net/bug.php?id=29992
http://bugs.php.net/bug.php?id=36240

And they were closed with bogus, saying to look at the manual.

Well, the manual doesn't talk about this issue at all, infact it seems to say that it should work as expected.

Also, that var should not exists beyond the scope of the foreach loop, and the developer that closed the bug seems to agree (he said for whatever weird reason, and honestly, php should cater to the normal reasons, not the weird ones, and anyone can get the same responce out of setting it manually in the foreach themselves, rather then allowing the engine to do something weird).

In anycase, this should at the very least get fixed up in the documentation, but I still consider this a engine bug.

Reproduce code:
---------------
<?php
$array = array(1,2,3);
foreach( $array as &$item );
foreach( $array as $item );
print_r( $array );
?>

Expected result:
----------------
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

Actual result:
--------------
Array
(
    [0] => 1
    [1] => 2
    [2] => 2
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-11 23:36 UTC] judas dot iscariote at gmail dot com
in bug 29992 read the "gardan at gmx dot de" comment for an explanation about why this is not bug.

However, the documentation should mention this behaviour ( but IMHO PHP should raise an E_STRICT in case this syntax is used for some weird reason )
 [2006-12-01 13:39 UTC] guillaume at cybercat dot ca
I also consider this a bug, here's a simpler test case. I consider an unset() should be executed on the "value" variable before starting the foreach.

Reproduce code:
---------------
<?php
$array = array(1,2,3);
$item = &$array[2];
foreach( $array as $item );
print_r( $array );
?>

Expected result:
----------------
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

Actual result:
--------------
Array
(
    [0] => 1
    [1] => 2
    [2] => 2
)
 [2007-08-17 22:03 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset()."
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Feb 17 05:01:25 2019 UTC