note 78931 added to function.count

From: Date: Fri, 02 Nov 2007 21:08:33 +0000
Subject: note 78931 added to function.count
Groups: php.notes 
Request: Send a blank email to php-notes+get-131953@lists.php.net to get a copy of this message
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. ---- Server IP: 209.41.74.194 Probable Submitter: 74.39.232.194 ---- Manual Page -- http://www.php.net/manual/en/function.count.php Edit -- https://master.php.net/note/edit/78931 Del: integrated -- https://master.php.net/note/delete/78931/integrated Del: useless -- https://master.php.net/note/delete/78931/useless Del: bad code -- https://master.php.net/note/delete/78931/bad+code Del: spam -- https://master.php.net/note/delete/78931/spam Del: non-english -- https://master.php.net/note/delete/78931/non-english Del: in docs -- https://master.php.net/note/delete/78931/in+docs Del: other reasons-- https://master.php.net/note/delete/78931 Reject -- https://master.php.net/note/reject/78931 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131953) next »