note 85331 added to function.range
| From: | ThinkMedical at Gmail dot com | Date: | Tue, 26 Aug 2008 12:11:42 +0000 |
| Subject: | note 85331 added to function.range | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143830@lists.php.net to get a copy of this message | ||
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()).
----
Server IP: 64.71.164.2
Probable Submitter: 84.19.108.52
----
Manual Page -- http://www.php.net/manual/en/function.range.php
Edit -- https://master.php.net/note/edit/85331
Del: integrated -- https://master.php.net/note/delete/85331/integrated
Del: useless -- https://master.php.net/note/delete/85331/useless
Del: bad code -- https://master.php.net/note/delete/85331/bad+code
Del: spam -- https://master.php.net/note/delete/85331/spam
Del: non-english -- https://master.php.net/note/delete/85331/non-english
Del: in docs -- https://master.php.net/note/delete/85331/in+docs
Del: other reasons-- https://master.php.net/note/delete/85331
Reject -- https://master.php.net/note/reject/85331
Search -- https://master.php.net/manage/user-notes.php