Re: [PHP-DEV] foreach nastiness with references (bug #21702)
Moriyoshi Koizumi wrote: I tried to answer this question in the bug report page. [...] 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? I probably don't see all the consequences (I know little about PHP/Zend internals and have found no documentation), but wouldn't the fix be as simple as changing the SEPARATE_ZVAL_IF_NOT_REF into SEPARATE_ZVAL on line 2251 of Zend/zend_execute.c? URL: http://lxr.php.net/source/php4/Zend/zend_execute.c#2251 (search for case ZEND_FE_RESET: if line numbers shift). BTW, what's the relation of http://lxr.php.net/source/php4/Zend/ and http://lxr.php.net/source/Zend/ ? Vaclav Dvorak [EMAIL PROTECTED] -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] foreach nastiness with references (bug #21702)
On Wed, Jan 22, 2003 at 04:12:18PM +0100, Vaclav Dvorak wrote: Moriyoshi Koizumi wrote: I tried to answer this question in the bug report page. [...] 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.devarticle=22668 http://news.php.net/article.php?group=php.devarticle=22672 http://news.php.net/article.php?group=php.devarticle=22673 It seems that the commit I mentioned in the previous mail was just for those problem reports. As no much discussion took place there, all I can guess from them is there was already a fear of breaking BC. Anyway, please search the database first, in order not to post the same kind of bug twice. Moriyoshi -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] foreach nastiness with references (bug #21702)
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.devarticle=22668 http://news.php.net/article.php?group=php.devarticle=22672 http://news.php.net/article.php?group=php.devarticle=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 -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DEV] foreach nastiness with references (bug #21702)
To answer part of your question: One more thing: whether this bug is fixed or not, the documentation must be clarified! I don't know about you, but I simply don't understand what it's supposed to say. Quoting: Note: Also note that foreach operates What the documentation means is that ?php foreach($foo as $key=$val) { $val = newval; } ? Will not change every element in the $foo array to newval. However, although $foo cannot be modified by modifying $key and $val the internal array pointer WILL increment as if you were actually working on that array directly.. So in order to restore the internal array pointer back to the start of the array you'd need a call to reset($foo) first. Side note: You can still modify the values within $foo by: ?php foreach($foo as $key=$val) { $foo[$key] = newval; } ? -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] foreach nastiness with references (bug #21702)
Hi, ?php $a = array(1,2,3); #$r = $a; echo current before foreach: . current($a) . br\n; foreach($a as $b) { echo - value in cycle: . $b . br\n; echo -- current in cycle: . current($a) . br\n; } echo current before foreach: . current($a) . br\n; ? Try it, and the delete the hash mark and try again. I tried to answer this question in the bug report page. To say more precisely, foreach statement always makes use of a copy of the given array instead of the original itself unless the array is a reference or has a reference. 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 -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DEV] foreach nastiness with references (bug #21702)
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 -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] foreach nastiness with references (bug #21702)
Just FYI, here's a relevant commit made by Andi two years ago: http://cvs.php.net/diff.php/Zend/zend_execute.c?r1=1.222r2=1.223ty=h Moriyoshi John Coggeshall [EMAIL PROTECTED] wrote: 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 -- PHP Development Mailing List http://www.php.net/ To unsubscribe, visit: http://www.php.net/unsub.php