#29686 [Opn->Asn]: Bug when using foreach on an array that has been referenced before

From: Date: Mon, 14 Feb 2005 23:22:57 +0000
Subject: #29686 [Opn->Asn]: Bug when using foreach on an array that has been referenced before
References: 1  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969366598@lists.php.net to get a copy of this message
ID: 29686 Updated by: tony2001@php.net Reported By: swalk at prp dot physik dot tu-darmstadt dot de -Status: Open +Status: Assigned -Bug Type: Scripting Engine problem +Bug Type: Documentation problem Operating System: * PHP Version: 5.* -Assigned To: +Assigned To: et New Comment: Stefan, please look into it. If it works as expected, than it's a docu problem, probably. Previous Comments: ------------------------------------------------------------------------ [2004-08-15 16:09:15] jakub dot phpbug at horky dot net Yes, sorry, now I see you are true. When I tested the example above I didn't try to comment out the line '$b =& $a;' so I thought the array is always using directly from foreach on each loop. So IMHO there is either badly written documentation or bad implementation (which is btw relevant to that two bugs I posted before). BUG UPDATE: Now I noticed that if you first unset() the $a variable before re-setting it to the string (or anything else), it is working properly. ------------------------------------------------------------------------ [2004-08-15 15:44:12] et@php.net I don't use register globals. But there are lots of servers which do. 1st, you're contradicting the Documentation, which clearly states that the array should be copied: "Note: Also note that foreach operates on a copy of the specified array and not the array itself." 2nd, you didn't specify the difference between using a reference variable and a normal variable. If you do foreach($array as $v) { array_push($array, 0); } modifying the array, it doesn't affect the loop. By your reasoning, it should? Please don't bogus it again, because it is either a problem with the documentation or with the implementation. ------------------------------------------------------------------------ [2004-08-15 15:42:29] jakub dot phpbug at horky dot net Sorry for mistyping the bug numbers: bug #28939 and bug #29687 ------------------------------------------------------------------------ [2004-08-15 15:40:53] jakub dot phpbug at horky dot net To et@php.net: > especially the part with "Note: Also note > that foreach operates on a copy of the specified > array and not the array itself." Sorry, but that especially this paragraph is very obscure, because a subsequent sentence states "However, the internal pointer of the original array is advanced with the processing of the array." Furthermore, this is partially subject of recently opened bug #28939 or #28939. But there I really don't see any problem. Coincidental overwriting of registered global is IMO really not bug but stupidity of the author :-) ------------------------------------------------------------------------ [2004-08-15 15:27:49] helly@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Don't use register globals. And don't use it for example because of this problem. Second, a reference variable and a normal variable do NOT result in foreach copying the whole array. Instaed not accessing the data by ref [foreach($ar as &$data)] only results in copying the data. Therefor the array is still ste same and if you change the array in the loop then obviously you affect the loop. ------------------------------------------------------------------------ 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 http://bugs.php.net/29686 -- Edit this bug report at http://bugs.php.net/?id=29686&edit=1

« previous php.doc (#969366598) next »