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

Your name:

Reply:


Posted by Schraalhans Keukenmeester on 05/21/07 20:28

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? I'd say it suggests
PHP's reference model isn't really based on references for one. But I may
be wrong. A real headscratcher. And this proves Jerry's point once again:
don't trust anything that's not explicitly stated in detail in the
documentation.

Just for fun, a candidate for 'most original and idiotic endless
memory gobbling loop':
$a = range(0,4);
$b = &$a;
$cnt=2;
foreach($a as $k => $v){
$a[$cnt++] = 5;
echo "$k=>$v\n";
}

Weird thing is it doesn't even produce an error on my box. It just stops
(I do assume however this is due to memory running out)

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

[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

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