note 78931 deleted from function.count by danbrown
| From: | danbrown@php.net | Date: | Fri, 09 Jan 2009 22:59:11 +0000 |
| Subject: | note 78931 deleted from function.count by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149007@lists.php.net to get a copy of this message | ||
Note Submitter: Wulfson at wulfden dot com
----
The last two comments (redbehelit and Jaik) are a little misguided. In a 26 element array,
for ($i = 0; $i < count($arr); $i++) {
works just fine, as count($arr) will return 26, which means that the loop will end as soon as $i
gets iterated to 26 (26 < 26 is false). You'd only have to use count($arr) - 1 if your
conditional was $i <=
Jaik is correct that it's better not to use count() inside the loop construct. As mentioned in
a previous comment, it'd be faster to use
$size = count($arr);
for ($i = 0; $i < $size; $i++) {
I don't agree that foreach should be used instead of this, however. In my trials, I found that
using a normal for loop was consistently faster than using a foreach (granted, not a whole lot
faster, but everything counts if you're trying to optimize). foreach really ought to only be
used when the array you're traversing contains associative indices.