Bug #29992 [Com]: foreach by reference corrupts the array

From: Date: Mon, 30 Apr 2018 03:33:29 +0000
Subject: Bug #29992 [Com]: foreach by reference corrupts the array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-214984@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=29992&edit=1 ID: 29992 Comment by: dan dot chase at chasetechnologysolutions dot com Reported by: fletch at pobox dot com Summary: foreach by reference corrupts the array Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: linux PHP Version: 5.0.1 Block user comment: N Private report: N New Comment: Sounds like another example things in PHP do not mirror other languages. If PHP doesn't have scoping, and we're all finding out about it years into our coding when we finally try to use foreach on a reference, not too bad.. however, I just corrected a report from LAST YEAR where we didn't know it was repeating a score in a sporting event because of this, I hope someone isn't too pissed they didn't get a trophy.. anyway, the lesson here kiddies, use a language you can trust! Previous Comments: ------------------------------------------------------------------------ [2017-06-09 15:45:03] qdinar at gmail dot com issue with var_dump shown at "[2011-07-13 06:48 UTC] martijn at twotribes dot com" is an independent thing; array element looks like it is somehow changed even if it is just only referenced, or it is shown by var_dump, ie might be var_dump shows an array element which is referenced from outside; i have googled about that issue and have found a comment at http://php.net/manual/en/language.references.php , "Dave at SymmetricDesigns dot com 9 years ago", (most voted) which shows that it is not only what is shown by var_dump, but the array element changes its behaviour. that issue is conceptually independent from explanations, which do not show the "&" in var_dump output, given here for this issue, because original array element needs not to become a reference itself to reproduce this issue, it is rewritten because it is referenced by $item. ------------------------------------------------------------------------ [2016-05-20 14:34:55] ourtusenka at yandex dot ru I have the same problem with php 5.6 on OpenServer. Before I never meet it; I don't use reference. But foreach do the same with my array. So: foreach ( $items as $item ) { //..echo single $item } Last element of $items is disappeared; ------------------------------------------------------------------------ [2015-01-01 20:18:52] chealer at gmail dot com The problematic behavior behing this issue is being tracked in #62132. ------------------------------------------------------------------------ [2014-12-06 23:33:13] dave dot mittner at gmail dot com Though this may not be a "bug" it's certainly dangerous and unexpected enough to deserve the level of importance. As has been said, people expect the behavior of block-level scoping. I don't come from Java or C++. I've programmed PHP for over a decade. I don't expect this kind of scope treatment because of other languages. I expect it because it's a clean and logical practice that's far safer than the contrary. Best practice for *any* language is to assume limited scope in blocks. I've known that since I was a teenager. In short, this should be changed. Even if it's a hack like unsetting it, that's safer than the current processing model. And I don't buy that "we can't change it because people use it" crap. I've had to rewrite entire systems due to changes in how PHP works. Stuff breaking is a risk any time you update PHP and why you should pay attention to change lists. ------------------------------------------------------------------------ [2014-06-20 22:09:56] steve at fancyguy dot com I have done this intentionally in some very lazy prototypes. foreach($data as $k => &v) { if (meets_complex_precondition($k)) { break; } } // do some stuff with $v potentially modifying it later unset($v); Comes in handy on occasion when working recursively by reference in deep arrays. Neat trick like 'array_map(null, $arr1, $arr2)'. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=29992 -- Edit this bug report at https://bugs.php.net/bug.php?id=29992&edit=1

« previous php.bugs (#214984) next »