Re: Possible foreach bug; seeking advice to isolate the problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux