NYCPHP Meetup

NYPHP.org

[nycphp-talk] by ref or by val? for vs foreach + as

Chris Bielanski Cbielanski at inta.org
Fri Jan 30 09:07:29 EST 2004


Thanks for the input :) It's one of those "am-I-losing-my-marbles?" sort of
situations and it's good to have feedback confirming my logic!

Thanks,
Chris Bielanski - [CBielanski at inta.org]
Web Programmer, 
International Trademark Association - [www.inta.org]
1133 Ave. of the Americas - Manhattan
p - 212/642-1745





-----Original Message-----
From: Dan Cech [mailto:dcech at phpwerx.net]
Sent: Thursday, January 29, 2004 8:55 PM
To: NYPHP Talk
Subject: Re: [nycphp-talk] by ref or by val? for vs foreach + as


Adam Maccabee Trachtenberg wrote:

> On Thu, 29 Jan 2004, Chris Bielanski wrote:
>>and foreach(&$array as $foo) should be right out - it's only one value -
the
>>reference of $array, yes/no?
> 
> I've never tried this, but I'm willing to bet it won't give you a
> by-reference iteration.

If you want to perform an operation on every member of an array using 
foreach, you can do:

foreach ( array_keys($array) as $key ) {
	$array[$key] =& myfunction($array[$key]);
}

As always there are any number of ways to skin a cat, if you just wanted 
to increment every member of an array you could do:

foreach ( $array as $key => $val ) {
	$array[$key]++;
}

which is equivalent to:

foreach ( array_keys($array) as $key ) {
	$array[$key]++;
}

Dan

_______________________________________________
talk mailing list
talk at lists.nyphp.org
http://lists.nyphp.org/mailman/listinfo/talk



More information about the talk mailing list