Re: [PHP4BETA] request: foreach ($array as $key => &$value)
| From: | Duncan McIntyre | Date: | Mon, 20 Dec 1999 15:54:05 +0000 |
| Subject: | Re: [PHP4BETA] request: foreach ($array as $key => &$value) | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-8227@lists.php.net to get a copy of this message | ||
OK. Now I am confused. The way the syntax currently works, '&' makes two
variables indissociable _aliases_ to each other, and not pointers, viz:
Example 1
==============
$a=1;
$b=&a;
$b=2;
==> now $a == 2
$c=3;
$b=&$c;
==> now $a,$b and $c == 3 !!
With a syntax like foreach($array as $key=>&$value) ....
we should expect that at the end of the loop all the members of $array would be
aliases to the last element of the array.
To me the whole syntax of "references" is highly ambiguous and inconsistant.
Since I imagine that no one expects the foreach loop above to set all the
elements of $array to the same value, I would equally imagine that people will
be confused behaviour of example 1 since there is no means to undissociate the
aliases created by simple assignment.
We have discussed the need for an unset() function to do this dissociation,
however I really think that it is more confusing than anything. Am I alone in
thinking that
$b=&$c
should not change the value referenced by $a? The '&' operator should do an
implicit unset(), as it does in the foreach() by ref.
We're all going to end up doing things like putting scalars into arrays just so
that we can take advantage of the fact the foreach() does unset() on each
iteration.
On Fri, 17 Dec 1999, Thies C. Arntzen wrote:
> On Fri, 17 Dec 1999, Andrei Zmievski wrote:
>
> > On Fri, 17 Dec 1999, Andi Gutmans wrote:
> > > At 02:48 AM 12/17/99 +0100, Thies C. Arntzen wrote:
> > > >zeev, andi -
> > > >
> > > >would it be hard to implement this syntax, currently there's no good way
> > > >of traversion an array *without* coping the values (very bad if you have
> > > >lot's of subarrays). also we should enable foreach for objects as well i
> > > >think...
> > >
> > > It has been a while since we have touched foreach(). We should maybe
> > > consider changing it from copying to being a reference. I remember this was
> > > problematic but I'll try and think about it. It probably does make more
> > > sense as a reference.
> >
> > I feel that for consistency's sake and other considerations, it would be
> > better to allow the user to specify whether foreach() uses copy or
> > reference. Can it be done that way instead of hard-coding how the
> > assignment is done?
> read the subject;-))
> foreach ($array as $key => &$value) -> by ref
> foreach ($array as $key => $value) -> normal..
>
>
> from kuala lumpur airport...
> tc
>
> >
> > -Andrei
> >
> > If you find a job that you love, you'll
> > never work another day in your life.
> > - Mark Jackson
> >
> >
>
>
> Thies C. Arntzen "One Big-Mac, Small Fries and a Coke!"
> Digital Collections Phone +49 40 235350 Fax +49 40 23535180
> Hammerbrookstr. 93 20097 Hamburg / Germany
>
>
> --
> PHP 4.0 Beta Mailing List <http://www.php.net/version4/>
> To unsubscribe, e-mail: php4beta-unsubscribe@lists.php.net
> For additional commands, e-mail: php4beta-help@lists.php.net
> To contact the list administrators, e-mail: php4beta-admin@lists.php.net
--
--------------------------------------------------------------------
Duncan McIntyre
Director
Emarketeers Ltd
Priory Gate, Priory Road
Dunstable, Beds. LU5 4HR
England
Duncan@Emarketeers.com
(+44) 1582 534 324 - Office
(+44) 7957 136 369 - Mobile