RE: [PHP-DEV] foreach nastiness with references (bug #21702)

From: Date: Wed, 22 Jan 2003 07:09:13 +0000
Subject: RE: [PHP-DEV] foreach nastiness with references (bug #21702)
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-93594@lists.php.net to get a copy of this message
Ah, I understand now... This perhaps in a documentation problem then after all, as there is no way to change this behavior cleanly that I can see... What about making a copy of the array and all of the references associated with that array instead of just using the real array? Just a thought. John >What I meant by this is > >1) Each time before entering a foreach loop, php first tries >to make a copy of > the array being iterated. > >2) In case the array variable is either referenceing another >variable or > referenced by another variable, no copy is made here and >the original > array is used instead. Because of this behaviour, the >original's internal > array pointer increases in the loop eventually. > >And once a variable is referenced by another, both are treated >as "reference" from then on. For example, > ><?php > $test = array(); > $test['a'] = 'a'; > $test['b'] = &$test['a']; > > debug_zval_dump($test); >?> > >This script produces following output: > >array(2) refcount(2){ > ["a"]=> > &string(1) "a" refcount(2) > ["b"]=> > &string(1) "a" refcount(2) >} > >Moriyoshi > >

« previous php.dev (#93594) next »