List Info

Thread: note 78535 added to function.count




note 78535 added to function.count
user name
2007-10-16 14:24:42
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  -- h
ttps://master.php.net/note/delete/78535/integrated
Del: useless     -- http
s://master.php.net/note/delete/78535/useless
Del: bad code    -- htt
ps://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     -- http
s://master.php.net/note/delete/78535/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78535
Reject      -- https://mast
er.php.net/note/reject/78535
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )