Voting

: nine minus eight?
(Example: nine)

The Note You're Voting On

ThinkMedical at Gmail dot com
16 years ago
foreach(range()) whilst efficiant in other languages, such as python, it is not (compared to a for) in php*.

php is a C-inspired language and thus for is entirely in-keeping with the lanuage aethetic to use it

<?php
//efficiant
for($i = $start; $i < $end; $i+=$step)
{
//do something with array
}

//inefficiant
foreach(range($start, $end, $step) as $i)
{
//do something with array
}
?>

That the officiant documentation doesnt mention the for loop is strange.

Note however, that in PHP5 foreach is faster than for when iterating without incrementing a variable.

* My tests using microtime and 100 000 iterations consistently (~10 times) show that for is 4x faster than foreach(range()).

<< Back to user notes page

To Top