note 78535 added to function.count
| From: | redbehelit at yahoo dot com | Date: | Tue, 16 Oct 2007 19:24:42 +0000 |
| Subject: | note 78535 added to function.count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131430@lists.php.net to get a copy of this message | ||
Be careful if you're using count in a loop. Example:
$arr ends at index number 25 (or $arr[25])
We know there are actually 26 elements when we include element 0 (as we should). This is what count
is going to return back. But if we do this:
for ($i = 0; $i < count($arr); i++) {
This loop is going to try to traverse to $arr[26] which does not exist. The last element is at index
number 25. We can fix this with a minor edit:
for ($i = 0; $i < count($arr)-1; i++) {
----
Server IP: 64.71.164.2
Probable Submitter: 69.117.103.75
----
Manual Page -- http://www.php.net/manual/en/function.count.php
Edit -- https://master.php.net/note/edit/78535
Del: integrated -- https://master.php.net/note/delete/78535/integrated
Del: useless -- https://master.php.net/note/delete/78535/useless
Del: bad code -- https://master.php.net/note/delete/78535/bad+code
Del: spam -- https://master.php.net/note/delete/78535/spam
Del: non-english -- https://master.php.net/note/delete/78535/non-english
Del: in docs -- https://master.php.net/note/delete/78535/in+docs
Del: other reasons-- https://master.php.net/note/delete/78535
Reject -- https://master.php.net/note/reject/78535
Search -- https://master.php.net/manage/user-notes.php