Richard Harb wrote:
Uh, I was wrong...

Foreach evaluates only once at initialisation... You can even unset
the array once into the loop and it still works perfectly well (not
that I recommend this as good practice or whatever, just did some
checking to be sure :)

Richard

That will work because foreach iterates over a copy of the array. I often do things like:


foreach($arr as $key => $val) {
  if(some condition) {
    unset($arr[$key]);
  }
}

The loop keeps running fine over its copy of the array while the original gets values taken out. You could also set new values if you wanted, of course, and those wouldn't get iterated over.



Friday, April 30, 2004, 9:55:14 AM, thus was written:

* Thus wrote Mattias Thorslund ([EMAIL PROTECTED]):

Just a quick follow-up question on that:

Curt Zirzow wrote:


foreach(array_slice($filenames, 0, 2) as $filename) {
$files[] = "path/to/" . $filelname;
}



Will a function (such as the array_slice here) in a foreach statement get re-evaluated for each iteration, or is it evaluated just once?


Only once.


In other words, is there anyhing gained by doing:

$short_array = array_slice($filenames, 0, 2);

foreach($short_array as $filename) {
$files[] = "path/to/" . $filelname;
}


Nothing really except mabey that it might be a little readable.



Curt
--
"I used to think I was indecisive, but now I'm not so sure."


--
paperCrane <Justin Patrin>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to