RE: [PHP4BETA] request: foreach ($array as $key => &$value)

From: Date: Mon, 20 Dec 1999 16:26:20 +0000
Subject: RE: [PHP4BETA] request: foreach ($array as $key => &$value)
References: 1  Groups: php.version4 
Request: Send a blank email to php-version4+get-8232@lists.php.net to get a copy of this message
> -----Original Message----- > From: Duncan McIntyre [mailto:duncan@emarketeers.com] > Sent: Monday, December 20, 1999 03:54 PM > To: Thies C. Arntzen; Andrei Zmievski > Cc: Andi Gutmans; php4beta@lists.php.net > Subject: Re: [PHP4BETA] request: foreach ($array as $key => &$value) > > > 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 !! You could then do: $d=4; $b=&$d; and $b and $d have for while $a and $c have 3 The fact is, the variables are not aliases to eachother, but pointing to the same "nameless" slot. (Once a slot really has no names pointing to it, it vanishes). Thus in your first example, $b does not become $a, but it becomes what $a currently is. $a=.... changes what is stored in what $a is. > 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. No, but you might reasonably expect $key to be an aliases to what the last element of the array points to. > To me the whole syntax of "references" is highly ambiguous and > inconsistant. It isn't though. > Since I imagine that no one expects the foreach loop above to set all the > elements of $array to the same value, it doesn't. > 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. I think unset($b) will, as will $b=&$somethingelse > We have discussed the need for an unset() function to do this > dissociation, why, won't unset do it? It does for me. > 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? It will not change the value referenced by $a > The '&' operator > should do an > implicit unset(), as it does in the foreach() by ref. I suppose it does if you want to think of it like that. > 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. You seem a little confused (for good reason no doubt). Think of a variable as a name for a slot. A slot stores data and MUST store data (no such thing as a magic "undef" or nul like in PERL or C). A variable is merely a reference to a slot. A slot can have many references. If a slot has no names, it is garbage collected and trashed. Example 1 ============== $a=1; # As $a does not reference a slot then make a slot; store 1 in the slot $b=&a; # Make $b reference to the same slot $a references to. This slot now has 2 references $b=2; # As be does reference a slot, just store 2 in the slot (same slot as $a references) > ==> now $a == 2 $c=3; # Make a new slot, reference it to $c and store 3 $b=&$c;# make $b now reference the same slot as $c does. This means the $a slot now only has one reference again Is this clear? Sam

« previous php.version4 (#8232) next »