![]() ![]() Otherwise we have to put a big red note in the manual to always unset variables after using the referential syntax or to use $long_descriptive_names_for_references, which is a little unreasonable in the current context. to unset the reference before proceeding, just like it resets the array pointer. IMHO, foreach has to clear the variables used for key-value pairs, i.e. to provide an easier way to traverse an array than the other loop constructs and not to touch other variables or references that are still pointing somewhere, unless it's done implicitly in the body of the loop. The main purpose of foreach itself is (as the name implies) to do something "for each" element of the array, i.e. ![]() Now any foreach in the other scripts might corrupt the base array without even knowing it - just because the name of the "value" variable is the same. Imagine there's such a "referential" loop in a library which is included by some other scripts and it operates on $_SESSION or some other important array. Although there's a user note in the manual, still this problem is not described in the documentation itself.Īnyway, I believe the behaviour of foreach in this case is extremely dangerous. I kind of understand what's the diference between getSlot() and getSlot() used within an if-loop (I guess the scope created by if-loop makes the difference), but I'm not sure this difference entitles PHP to alter my array.Ĭan anyone explain this to me? Is it a bug or a regular behaviour? (Or did I get the ampersand-sign wrong?) The problem is that I loose control over the REAL objects and the elements get changed unreasonably later in the script. output C elements have been changed to referencesĪs you can see in output "C" the second iteration altered elements of the array - the elements have been turned into references. Yaroukh at email dot cz I believe the problem I'm gonna describe here has the same roots as the one fletch has described.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |