note 53952 deleted from control-structures.foreach by danbrown
| From: | danbrown@php.net | Date: | Tue, 28 Apr 2009 13:09:45 +0000 |
| Subject: | note 53952 deleted from control-structures.foreach by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153921@lists.php.net to get a copy of this message | ||
Note Submitter: JaGx
----
On the note of Paul's for loop:
function foo($x) {
global $arr; // some Array
for($i=0; $i < count($arr); $i++) {
if($arr[$i][0] == $x) {
echo $arr[$i][1]."\n";
foo($arr[$i][0]);
}
}
}
------------
The middle part of the for loop is evaluated every time it loops which means the count function is
called as many times as it loops. It's always better (performance/speed wise) to put the count
as the initialization code:
function foo($x) {
global $arr; // some Array
for($i=count($arr)-1;; $i>=0; $i++) {
if($arr[$i][0] == $x) {
echo $arr[$i][1]."\n";
foo($arr[$i][0]);
}
}
}