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

From: Date: Mon, 20 Dec 1999 20:55:01 +0000
Subject: RE: [PHP4BETA] request: foreach ($array as $key => &$value)
References: 1  Groups: php.version4 
Request: Send a blank email to php-version4+get-8247@lists.php.net to get a copy of this message
> 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? Yes, but confusing when used with objects, though probably not so if you're used to working with languages which have explicit syntax for dealing with pointers. Compare the output of the three bits of code below. I admit that I hadn't tried the intermediate null assignment in Example 3 before in order to achieve the unset() that I want. Now that I know it works I'm happy - and even happier that we're going to get pointers as return values from functions. Example 1 ========= <script language=Javascript1.2> a=new Array(); a[0]=1; b=a; a=new Array(); a[0]=3; alert("A:"+a[0]+" B:"+b[0]); </script> Example 2 ========== <? class test { var $a=1; } $a=new test; $b=&$a; $a=new test; $a->a=3; echo "A:".$a->a." B:".$b->a; Example 3 ========= $a=new test; $b=&$a; $c=0; $a=&$c; $a=new test; $a->a=3; echo "A:".$a->a." B:".$b->a."<BR>"; > > Sam > > > -- > 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

« previous php.version4 (#8247) next »