The foreach loop isn't 'unsetting' your value - it's just clobbering it. And I wouldn't say it's inconsistent.
foreach($array as &$reference) means "make $reference a reference to each member of the array in turn". foreach($tags as $value) means "make $value a copy of each value in the array in turn". So, assuming that $value happens to already be a reference to some place in the array (as is the case in the second loop) you're telling it "replace whatever is at the end of this reference with each value in the array in turn". That much you've already figured out, I gather. It's then pretty obvious why using a reference in the second loop _doesn't_ clobber the value: it just changes where the reference is pointed, not the actual value. I'd say this behaviour is logical, consistent and expected. Dom On Mon, Oct 20, 2008 at 12:30 AM, Mark S. <[EMAIL PROTECTED]> wrote: > > Hi, Steven. > Yes, I do agree with you that, unsetting values in such scenarios like > loops is something rather disappointing to see in modern day languages > and it does not seem logical. But, the documentation has a similar > example and the reason written right on the foreach page. And, it also > puzzles me as to why it works perfectly fine when a reference > ampersand is used in both the loops, I would say it's inconsistent in > it's implementation or maybe we both are missing some hidden memory > management concept. :-) > > Regards, > Mark S. > > > > On Oct 19, 10:47 pm, Steven Gilberd <[EMAIL PROTECTED]> wrote: >> Hi Mark, >> >> >> >> That makes sense - thanks. It just doesn't follow what should be logical >> behaviour for the foreach() operator - I would expect that to implicitly >> unset() $currtag on each iteration before assigning a new value to it, as >> nuking existing data constructs seems to be a rather braindead way of >> implementing it. >> >> >> >> Noting what you've found, do you have any idea why using a pointer there >> rather than a variable doesn't also trigger the issue? It seems rather >> inconsistant. Do you reckon this warrants an 'unexpected bahaviour' bug >> report? >> >> >> >> Cheers, >> >> Steve >> >> >> -----Original message----- >> From: Mark S. <[EMAIL PROTECTED]> >> Sent: Sun 19-10-2008 20:41 >> To: NZ PHP Users Group <[email protected]>; >> Subject: [phpug] Re: Bug in foreach()? >> >> Hi, Steven G. >> I think, I have found out why the code was outputting 'Item1' on each >> execution on the second foreach. >> Here is what, I did to the code - >> <?php >> $tags = array( >> 'Item1', >> 'Item2' >> ); >> //print_r($tags); >> foreach($tags as &$currtag) { >> $currtag = array($currtag, 0); >> } >> unset($currtag); //Unset the value ********** >> echo "<BR>"; >> print_r($tags); >> echo "<BR>"; >> //print_r($tags); >> foreach($tags as $currtag) { >> //print_r($currtag); >> print_r($tags); >> >> } >> >> ?> >> If I am correct then, as per the PHP documentation, reference of a >> $value($currtags) and the last array element remain even after the >> foreach loop. It is recommended to destroy it by unset(). >> This can be proved by changing the $currtag variable to something else >> in the second foreach loop e.g. >> <?php >> $tags = array( >> 'Item1', >> 'Item2' >> ); >> //print_r($tags); >> foreach($tags as &$currtag) { >> $currtag = array($currtag, 0); >> } >> //unset($currtag); //Unset the value ********** >> echo "<BR>"; >> print_r($tags); >> echo "<BR>"; >> //print_r($tags); >> foreach($tags as $currtag_1) { >> //print_r($currtag); >> print_r($tags); >> >> } >> >> ?> >> The second loop was using the previous value of $currtag. I hope this >> clears the doubt even though, it does seem odd and a bit illogical at >> first. >> >> Cheers, >> Mark S. >> >> On Oct 19, 5:00 pm, Steven Gilberd <[EMAIL PROTECTED]> wrote: >> > Hi, >> >> > While programming I stumbled across this - can anyone reproduce this, or >> > is it just a bug in my system? Replacing the $currtag in the final foreach >> > loop with &$currtag causes it to behave as expected, but the code as >> > pasted below is obviously badly dysfunctional. Is anyone able to shed some >> > light on this? >> >> > Cheers, >> >> > Steve >> >> > ------------------------------------- >> >> > <?php >> >> > $tags = array( >> > 'Item1', >> > 'Item2' >> > ); >> >> > foreach($tags as &$currtag) { >> > $currtag = array($currtag, 0); >> >> > } >> >> > print_r($tags); >> > foreach($tags as $currtag) { >> > print_r($tags); >> >> > } >> >> > ?> >> > ---------------------------------- >> >> > That code gives the following output: >> >> > Array >> > ( >> > [0] => Array >> > ( >> > [0] => Item1 >> > [1] => 0 >> > ) >> >> > [1] => Array >> > ( >> > [0] => Item2 >> > [1] => 0 >> > ) >> >> > ) >> > Array >> > ( >> > [0] => Array >> > ( >> > [0] => Item1 >> > [1] => 0 >> > ) >> >> > [1] => Array >> > ( >> > [0] => Item1 >> > [1] => 0 >> > ) >> >> > ) >> > Array >> > ( >> > [0] => Array >> > ( >> > [0] => Item1 >> > [1] => 0 >> > ) >> >> > [1] => Array >> > ( >> > [0] => Item1 >> > [1] => 0 >> > ) >> >> > ) > > > --~--~---------~--~----~------------~-------~--~----~ NZ PHP Users Group: http://groups.google.com/group/nzphpug To post, send email to [email protected] To unsubscribe, send email to [EMAIL PROTECTED] -~----------~----~----~----~------~----~------~--~---
