NYCPHP Meetup

NYPHP.org

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

Chris Bielanski Cbielanski at inta.org
Thu Jan 29 17:00:44 EST 2004


Can I get confirmation on the details of reference passing when using a for
loop as opposed to a foreach loop?

It's clearly documented that foreach proxies the array being iterated. Does
that behavior hold true when the array is passed in as a class member, ie
foreach($foo->array as $bar)? 
I can't imagine it would act differently but I'm getting conflicted output
in my own code.

So just to make sure I'm crystal clear on this..

In a for loop (assume $i is the iterator and $array the array) one might
assign to a local variable $foo = $array[$i]. At least I assume, based on
doc and experience, that this is by-value. The by-ref operation would be
$foo =& $array[$i], yes/no?

With a foreach - foreach ($array as $foo) -  we are talking about
*precisely* the same operation under differing syntax, yes/no?

and in both cases, $foo = 'bar' DOES NOT DO $array[$i] = 'bar', yes/no?

and foreach(&$array as $foo) should be right out - it's only one value - the
reference of $array, yes/no?

I won't argue - this should be intuitive, but as I said, I'm getting
conflicting output and I am not that good at writing up these questions!! :)

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







More information about the talk mailing list