Re: Unsetting loop variables at the end of the loop
| From: | Mike Wacker | Date: | Sun, 27 Dec 2009 18:46:21 +0000 |
| Subject: | Re: Unsetting loop variables at the end of the loop | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-46530@lists.php.net to get a copy of this message | ||
Rasmus Lerdorf wrote:
We can't just randomly reset variables based on their scope in this one specific case. If we are going to "fix" this, it should be done by introducing a way to do proper local scope variables. Resetting a reference simply because it is convenient in this one case would be completely inconsistent. -RasmusI have some thoughts on this for PHP 6, but here's something else that might be worth looking into. When you write [foreach ($ii as $i) ...], you usually expect that $i is not a reference variable (you would have used &$i otherwise). However, PHP allows this even if $i is already a reference variable (e.g.: it was used as such in a previous loop). Now look at this code: function f(&$x) { echo $x; } $x = 1; $args = array($x); call_user_func_array('f', $args); In PHP 5.3, this would trigger a warning and cause $x to be NULL inside of f because you mixed value and reference types in an improper way. This foreach() issue is essentially being caused by a similar evil mix of reference and value types. Perhaps [foreach ($ii as $i)] should trigger a warning if $i already exists as a reference variable.