[PHP-DEV] bug when using foreach with references?

2009-10-21 Thread Richard K Miller
Is this a bug in PHP? // Output: Array ( [0] => apple [1] => banana [2] => carrot ) Array ( [0] => apple [1] => banana [2] => carrot ) Array ( [0] => apple [1] => banana [2] => banana ) Two bananas in the last set?! Not what I expected. Richard -

Re: [PHP-DEV] bug when using foreach with references?

2009-10-21 Thread Jille Timmermans
Richard K Miller schreef: > Is this a bug in PHP? No > > $items = array('apple', 'banana', 'carrot'); > print_r($items); > foreach ($items as &$item) { } $item is now a reference to the last element of $items. > print_r($items); > foreach ($items as $item) { } And here, the foreach loop writes to

Re: [PHP-DEV] bug when using foreach with references?

2009-10-21 Thread Richard K Miller
Hi Jill, $item is now a reference to the last element of $items. print_r($items); foreach ($items as $item) { } And here, the foreach loop writes to $item for each element: thus assiging that value to the last element of $items. The last element of $items is 'carrot'. Why does it print appl

Re: [PHP-DEV] bug when using foreach with references?

2009-10-21 Thread mm w
Richard, enumerator exhausted, repeat your sub-sequence again you will get it e.g pointer and pointed Best Regards On Wed, Oct 21, 2009 at 7:01 PM, Richard K Miller wrote: > Hi Jill, > >>> >> $items = array('apple', 'banana', 'carrot'); >>> print_r($items); >>> foreach ($items as &$item) { } >>

Re: [PHP-DEV] bug when using foreach with references?

2009-10-21 Thread Marco Tabini
On 2009-10-21, at 10:40 PM, Richard K Miller wrote: I don't follow. Is this really the intended behavior? It seems quite unintuitive that the original array would be modified by *empty* loops. It is intended behaviour. Consider your code; at the end of this loop: $items = array('apple',

Re: [PHP-DEV] bug when using foreach with references?

2009-10-21 Thread Richard K Miller
I don't follow. Is this really the intended behavior? It seems quite unintuitive that the original array would be modified by *empty* loops. Suppose I create an include file that loops through $_GET variables with references: foreach ($_GET as &$get) { /* empty loop */ } Subsequent foreach(

Re: [PHP-DEV] bug when using foreach with references?

2009-10-22 Thread Antony Dovgal
On 22.10.2009 05:37, Richard K Miller wrote: > Is this a bug in PHP? Search the bug DB before asking such questions. http://bugs.php.net/bug.php?id=29992 http://bugs.php.net/bug.php?id=39307 http://bugs.php.net/bug.php?id=40065 http://bugs.php.net/bug.php?id=40654 http://bugs.php.net/bug.php?id=4

Re: [PHP-DEV] bug when using foreach with references?

2009-10-22 Thread Richard K Miller
On Oct 22, 2009, at 1:42 AM, Antony Dovgal wrote: On 22.10.2009 05:37, Richard K Miller wrote: Is this a bug in PHP? Search the bug DB before asking such questions. http://bugs.php.net/bug.php?id=29992 http://bugs.php.net/bug.php?id=39307 http://bugs.php.net/bug.php?id=40065 http://bugs.php.

Re: [PHP-DEV] bug when using foreach with references?

2009-10-22 Thread Rasmus Lerdorf
Richard K Miller wrote: > On Oct 22, 2009, at 1:42 AM, Antony Dovgal wrote: > >> On 22.10.2009 05:37, Richard K Miller wrote: >>> Is this a bug in PHP? >> >> Search the bug DB before asking such questions. >> >> http://bugs.php.net/bug.php?id=29992 >> http://bugs.php.net/bug.php?id=39307 >> http:/

Re: [PHP-DEV] bug when using foreach with references?

2009-10-22 Thread Richard K Miller
It is one of these things that make perfect sense when you think about it a little bit. Yes, it catches some people, just like strpos() returning character position 0 on a first-char match catches some people. There is no way to fix things like these without completely breaking things. If we ha

Re: [PHP-DEV] bug when using foreach with references?

2009-10-22 Thread jeffg
On 09-10-21 07:47 PM, Marco Tabini wrote: On 2009-10-21, at 10:40 PM, Richard K Miller wrote: I don't follow. Is this really the intended behavior? It seems quite unintuitive that the original array would be modified by *empty* loops. It is intended behaviour. Consider your code; at the end

Re: [PHP-DEV] bug when using foreach with references?

2009-10-22 Thread Rasmus Lerdorf
Richard K Miller wrote: >> It is one of these things that make perfect sense when you think about >> it a little bit. Yes, it catches some people, just like strpos() >> returning character position 0 on a first-char match catches some >> people. There is no way to fix things like these without co

Re: [PHP-DEV] bug when using foreach with references?

2009-10-22 Thread Richard K Miller
It would be arbitrarily breaking an explicit reference. I know I have code lying around that relies on multiple loops cleaning up a big complicated multi-level array. I do ugly things with references into that array and it would completely break if PHP magically deleted my references whether the

Re: [PHP-DEV] bug when using foreach with references?

2009-11-16 Thread Mathieu Suen
It would be arbitrarily breaking an explicit reference. I know I have code lying around that relies on multiple loops cleaning up a big complicated multi-level array. I do ugly things with references into that array and it would completely break if PHP magically deleted my references whether the

Re: [PHP-DEV] bug when using foreach with references?

2009-11-16 Thread Pierre Joye
On Mon, Nov 16, 2009 at 12:07 PM, Mathieu Suen wrote: > • Pensez à l'environnement, n'imprimez cet e-mail qu'en cas de réelle > nécessité Discussing endlessly an issue only because you do not understand it is also an environmental problem, please consider to read the manual and stop to use every

Re: [PHP-DEV] bug when using foreach with references?

2009-11-16 Thread Derick Rethans
On Mon, 16 Nov 2009, Mathieu Suen wrote: > > > It would be arbitrarily breaking an explicit reference. I know I > > > have code lying around that relies on multiple loops cleaning up a > > > big complicated multi-level array. I do ugly things with > > > references into that array and it would

Re: [PHP-DEV] bug when using foreach with references?

2009-11-16 Thread Mathieu Suen
Pierre Joye a écrit : On Mon, Nov 16, 2009 at 12:07 PM, Mathieu Suen wrote: • Pensez à l'environnement, n'imprimez cet e-mail qu'en cas de réelle nécessité Discussing endlessly an issue only because you do not understand it is also an environmental problem, please consider to read the manual

Re: [PHP-DEV] bug when using foreach with references?

2009-11-16 Thread Etienne Kneuss
Hello, On Mon, Nov 16, 2009 at 2:10 PM, Mathieu Suen wrote: > Pierre Joye a écrit : > > On Mon, Nov 16, 2009 at 12:07 PM, Mathieu Suen >> wrote: >> >> • Pensez à l'environnement, n'imprimez cet e-mail qu'en cas de réelle >>> nécessité >>> >> >> Discussing endlessly an issue only because you do

Re: [PHP-DEV] bug when using foreach with references?

2009-11-16 Thread Mathieu Suen
Etienne Kneuss a écrit : Hello, On Mon, Nov 16, 2009 at 2:10 PM, Mathieu Suen wrote: Pierre Joye a écrit : On Mon, Nov 16, 2009 at 12:07 PM, Mathieu Suen wrote: • Pensez à l'environnement, n'imprimez cet e-mail qu'en cas de réelle nécessité Discussing endlessly an issue only because y

Re: [PHP-DEV] bug when using foreach with references?

2009-11-16 Thread Victor Bolshov
That's a completely useless discussion, isn't it? Whoever is "right" with his opinion regarding scoping - nobody will (and nobody would like to) change PHP's scoping for those reasons as it would break too much existing code. 2009/11/16 Mathieu Suen : > Etienne Kneuss a écrit : >> >> Hello, >> >>

Re: [PHP-DEV] bug when using foreach with references?

2009-11-16 Thread Mathieu Suen
Maybe you find it useless. That is just a discussion. I don't expect PHP to change thought it should IMHO, especially now that you are introducing closure. :) For those who complain about the foreach behavior (including me) that's typically a scope issue. Doing an automatic unset isn't the rig

Re: [PHP-DEV] bug when using foreach with references?

2009-11-16 Thread Etienne Kneuss
Hello, On Mon, Nov 16, 2009 at 2:51 PM, Mathieu Suen wrote: > Etienne Kneuss a écrit : > > Hello, >> >> On Mon, Nov 16, 2009 at 2:10 PM, Mathieu Suen > >wrote: >> >> Pierre Joye a écrit : >>> >>> On Mon, Nov 16, 2009 at 12:07 PM, Mathieu Suen >>> wrote: • Pensez à l'environnem

Re: [PHP-DEV] bug when using foreach with references?

2009-11-16 Thread Richard Quadling
2009/11/16 Mathieu Suen : > Maybe you find it useless. That is just a discussion. I don't expect PHP to > change thought it should IMHO, especially now that you are introducing > closure. :) > > For those who complain about the foreach behavior (including me) that's > typically a scope issue. Doing

Re: [PHP-DEV] bug when using foreach with references?

2009-11-16 Thread Mathieu Suen
Etienne Kneuss a écrit : Hello, On Mon, Nov 16, 2009 at 2:51 PM, Mathieu Suen wrote: Etienne Kneuss a écrit : Hello, On Mon, Nov 16, 2009 at 2:10 PM, Mathieu Suen *SKIP* Static scoping is closely related to variable declaration. In PHP, there is no such thing as a variable declaration st