ID:               50161
 User updated by:  marc at perkel dot com
 Reported By:      marc at perkel dot com
 Status:           Bogus
 Bug Type:         Scripting Engine problem
 Operating System: Linux
 PHP Version:      5.2.11
 New Comment:

Give me an example of code it would break if you deleted the reference
at the beginning of a foreach loop. I'm not suggesting that it be
deleted at the end. And foreach will delete a variable if it is already
set to a value. For example:

$y = "some test";

foreach ($myarray as $y) {
   print "$y\n";
}


In this case $y is overridden. So it is inconsistent not to override a
reference at the beginning of foreach.

There are other cases where references are deleted. If you do:

unset($x);

It unsets $x - not what $x is pointing to.

The point is - the results of the example I posts here makes PHP
laughable out here in the real world. I think it's a bad idea for PHP to
fail the laugh test.


Previous Comments:
------------------------------------------------------------------------

[2009-11-12 22:56:36] ras...@php.net

Arbitrarily deleting a reference would break a lot of code.  What you
are looking for a block-scope variables.  We do not have those in PHP.

------------------------------------------------------------------------

[2009-11-12 21:44:54] marc at perkel dot com

If it's been reported several times then you aren't listening. It is a
bug. This is why open source has a bad name because people don't fix
what is obviously a bug.

------------------------------------------------------------------------

[2009-11-12 21:36:55] j...@php.net

Already reported several times, already decided to be the correct
behavior which is also documented. 

------------------------------------------------------------------------

[2009-11-12 20:45:33] marc at perkel dot com

Description:
------------
When using foreach and looping through an array the second time if the
index variable isn't unset the results are that the referenced variables
is used as the index rather than the named variable.

The issue can be solved if when the foreach is set up that it does an
unset on the variable passed as the "as" variable. PHP should be changed
to unset the parameter passed as the index into the array.


Reproduce code:
---------------
$myarray = array("one","two","three","four");

foreach ($myarray as &$x) {
   $x = "$x -";
   print "$x\n";
}

print "\n";

foreach ($myarray as $x) {
   print "$x\n";
}


Expected result:
----------------
one -
two -
three -
four -

one -
two -
three -
four -

Actual result:
--------------
one -
two -
three -
four -

one -
two -
three -
three -


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=50161&edit=1

Reply via email to