Reply to Re: safe to delete elements of array in foreach

Your name:

Reply:


Posted by gosha bine on 05/22/07 08:26

On 21.05.2007 22:28 Schraalhans Keukenmeester wrote:
> At Mon, 21 May 2007 19:39:43 +0200, gosha bine let his monkeys type:
>
>
>> Good point. Here's another example for even deeper meditation
>>
>> $a = range(0,4);
>> // $b = &$a;
>> foreach($a as $k => $v){
>> $a[99] = 5;
>> echo "$k=>$v\n";
>> }
>>
>> (try uncommenting the second line).
>
> Yikes. Nice. Now let us get this straight. By referencing ($b = &$a)
> suddenly the foreach loop becomes aware of changes in $a. Tricky.
>
> Any solid ideas how this works internally?

Internally, foreach is a pair of FE_RESET (start loop) and FE_FETCH
(iteration) opcodes, implementation of FE_RESET starts here

http://lxr.php.net/source/ZendEngine2/zend_vm_def.h#3191

note the line 3219
(http://lxr.php.net/source/ZendEngine2/zend_vm_def.h#3219)

SEPARATE_ZVAL_IF_NOT_REF(array_ptr_ptr);


It basically says: "separate ZVAL" (i.e. make a copy) of the array
(array_ptr_ptr) only if it is not a reference. That is, Rami was right
about copying, but it only takes place when the array is a "normal"
variable, not a reference.

> <...>
> Again, impressive albeit academic find, Gosha. Worth a bug report you
> think? Sh.

I'm kind of reluctant to upset the good people ;) therefore I'd refrain
from reporting this. Maybe you? ;)


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация