Re: Unsetting loop variables at the end of the loop
| From: | jvlad | Date: | Mon, 28 Dec 2009 10:44:37 +0000 |
| Subject: | Re: Unsetting loop variables at the end of the loop | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-46546@lists.php.net to get a copy of this message | ||
> jvlad wrote:
>>> $a = array(1);
>>> $b = 0;
>>> $c = &$b;
>>> foreach($a as $c);
>>> Now you are arguing that $b should not be 1?
>>> The two pieces of code are identical
>>
>> It's just a nightmare example. I wonder have you ever see anything like
>> this
>> in real life?
>> Could you please let me see it too, for example in code.google.com?
>> If you cann't, what are you fighting for?
>> Interestingly, how many people will consider the code sample you
>> demonstrated ugly...
>> How many sporadic problems were already created and will be created just
>> beacause
>> of this unclear behavior of foreach?
>
> It isn't unclear. It is perfectly consistent. Whether it is ugly or
> not is irrelevant.
Even though you can kill somebody using hammer, does it mean that it _is_
the feature to
be preserved in all future versions of the hammer?
Also, you didn't answer why another function that is WIDELY used, is going
to hell.
Why don't you defend its features which go to hell with this function? Why
don't you
defent the language BC in this case? What does make your position so
selective?
Why a useless and harmful (yes it's harmful according to so many posts)
feature is preserved
while uselfull and needed function is going away?
>
>>> And I don't see how your prev/next stuff has anything to do with this.
>>
>>
>> Ok. What my stuff has to do with this is there:
>> If prev/next were consistent with foreach, they would return first/last
>> element of the
>> array respectively as soon as they reached the boundary. But no! They
>> return
>> FALSE.
>
> Which has nothing to do with the question at hand here. We are talking
> about breaking any reference in the variables used in the loop
> construct. We are not talking about any other behaviour here.
Please have a look at this page
http://www.php.net/manual/en/control-structures.foreach.php
It's where you can find the following:
------------------------------------------------------------------------------
You may have noticed that the following are functionally identical:
<?php
$arr = array("one", "two", "three");
reset($arr);
while (list(, $value) = each($arr)) {
echo "Value: $value<br />\n";
}
foreach ($arr as $value) {
echo "Value: $value<br />\n";
}
?>
------------------------------------------------------------------------------
I checked and it appears that they are not identical, quite the opposite,
they expose the
difference I mentioned before.
Each() assigns NULL to the value after the array pointer reached end.
ForEach() stays with last element in this case:
compare the output:
<?php
$arr = array("one", "two", "three");
reset($arr);
while (list(, $value) = each($arr)) {
echo "Value: $value<br />\n";
}
var_dump($value);
?>
<?php
$arr = array("one", "two", "three");
foreach ($arr as $value) {
echo "Value: $value<br />\n";
}
var_dump($value);
?>
-jv