RE: [PHP] Possible foreach bug; seeking advice to isolate the problem
-Original Message- From: Jonathan Sachs [mailto:081...@jhsachs.com] Sent: 20 October 2010 04:48 To: php-general@lists.php.net Subject: [PHP] Possible foreach bug; seeking advice to isolate the problem I've got a script which originally contained the following piece of code: foreach ( $objs as $obj ) { do_some_stuff($obj); } When I tested it, I found that on every iteration of the loop the last element of $objs was assigned the value of the current element. I was able to step through the loop and watch this happening, element by element. All the other suggestions I've seen on this are essentially correct -- before the foreach runs, something somewhere has set $obj to be a reference to the last element of the array. It really doesn't matter whether you can find the culprit for this or not -- the solution is simply to put an unset($obj) immediately before the foreach statement -- this will break the reference (without destroying anything but $obj!) and make the foreach behave exactly as you want. Cheers! Mike -- Mike Ford, Electronic Information Developer, Libraries and Learning Innovation, Leeds Metropolitan University, C507 City Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Possible foreach bug; seeking advice to isolate the problem
On 20/10/2010 05:47, Jonathan Sachs wrote: I've got a script which originally contained the following piece of code: foreach ( $objs as $obj ) { do_some_stuff($obj); } When I tested it, I found that on every iteration of the loop the last element of $objs was assigned the value of the current element. I was able to step through the loop and watch this happening, element by element. Are you are using a 'referencing' foreach? i.e. foreach ($objs as $obj) { do_some_stuff($obj); } or is the above code a direct lift from your script? Referencing foreach statements can cause problems as the reference to the last array entry is persistent after the foreach loop has terminated so any further foreach statements on the same array will overwrite the previous reference which is still pointing to the last item. Rich -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Possible foreach bug; seeking advice to isolate the problem
I've got a script which originally contained the following piece of code: foreach ( $objs as $obj ) { do_some_stuff($obj); } When I tested it, I found that on every iteration of the loop the last element of $objs was assigned the value of the current element. I was able to step through the loop and watch this happening, element by element. I originally encountered this problem using PHP v5.2.4 under Windows XP. I later reproduced it in v5.3.2 under XP. The function call wasn't doing it. I replaced the function call with an echo statement and got the same result. For my immediate needs, I evaded the problem by changing the foreach loop to a for loop that references elements of $objs by subscript. That leaves me with the question: what is going wrong with foreach? I'm trying to either demonstrate that it's my error, not the PHP engine's, or isolate the problem in a small script that I can submit with a bug report. The current script isn't suitable for that because it builds $objs by reading a database table and doing some rather elaborate manipulations of the data. I tried to eliminate the database by doing a var_export of the array after I built it, then assigning the exported expression to a variable immediately before the foreach. That broke the bug -- the loop behaved correctly. There's a report of a bug that looks similar in the comments section of php.net's manual page for foreach, time stamped 09-Jul-2009 11:50. As far as I can tell it was never submitted as a bug and was never resolved. I sent an inquiry to the author but he didn't respond. Can anyone make suggestions on this -- either insights into what's wrong, or suggestions for producing a portable, reproducible example? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php