Re: foreach nastiness with references (bug #21702)

From: Date: Wed, 22 Jan 2003 23:04:20 +0000
Subject: Re: foreach nastiness with references (bug #21702)
References: 1 2 3 4 5 6  Groups: php.dev 
Request: Send a blank email to php-dev+get-93601@lists.php.net to get a copy of this message
Moriyoshi Koizumi wrote:
Moriyoshi Koizumi wrote:
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.
Yes, I understand this. What I don't understand is WHY is it so? Why is foreach handling references specially? Why is your point 2) there?
Oh, I found this issue was pointed out pretty long time ago. - Bug #5052 http://bugs.php.net/5052 - Bug #5270 (deleted?) http://news.php.net/article.php?group=php.dev&article=22668
    http://news.php.net/article.php?group=php.dev&article=22672
      http://news.php.net/article.php?group=php.dev&article=22673
[...]
Anyway, please search the database first, in order not to post the same kind of bug twice.
I did search the database, and indeed I did see that bug (#5052) before posting my bug report. In fact, if you read my bug report, you will see that I even mention this bug, and yet another one (#14607). The bug you now found (#5052) is similar but NOT the same. In fact, it describes the opposite behaviour: the reporter there says that nested foreach's on two COPIES of the same array don't work, and that he can work around the bug by making a reference to the array. In my bug, nested foreach on a copy works, but not on a reference. So there, I'm not such a complete idiot, you know. ;-) Regards, Vaclav Dvorak

« previous php.dev (#93601) next »