Unsetting loop variables at the end of the loop

From: Date: Sun, 27 Dec 2009 12:56:22 +0000
Subject: Unsetting loop variables at the end of the loop
Groups: php.internals 
Request: Send a blank email to internals+get-46523@lists.php.net to get a copy of this message
PHP's documentation for foreach states that if you iterate by reference [foreach ($ii as &$i) ...], you should unset $i after the loop.  $i still points to the last element of the array - updating $i or reusing it will update the last element of the array.

In short, why doesn't PHP automatically unset $i after the loop?  I can't think of too many cases where you would want to hold on to that reference after you exit the loop, but I can think of a lot of scenarios where a user could accidentally tamper the array by using $i in a different context later on (especially since loop variable names often are reused).


Thread (20 messages)

« previous php.internals (#46523) next »