note 117224 added to function.count

From: Date: Tue, 05 May 2015 14:18:46 +0000
Subject: note 117224 added to function.count
Groups: php.notes 
Request: Send a blank email to php-notes+get-202445@lists.php.net to get a copy of this message
As I see in many codes, don't use count to iterate through array. Onlyranga says you could declare a variable to store it before the for loop. I agree with his/her approach, using count in the test should be used ONLY if you have to count the size of the array for each loop. You can do it in the for loop too, so you don't have to "search" where the variable is set. e.g. <?php $array = [1, 5, 'element']; for($i = 0, $c = count($array); $i < $c; $i++) var_dump($array[$i]); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 88.186.20.115 ---- Manual Page -- http://php.net/manual/en/function.count.php Edit -- https://master.php.net/note/edit/117224 Del: integrated -- https://master.php.net/note/delete/117224/integrated Del: useless -- https://master.php.net/note/delete/117224/useless Del: bad code -- https://master.php.net/note/delete/117224/bad+code Del: spam -- https://master.php.net/note/delete/117224/spam Del: non-english -- https://master.php.net/note/delete/117224/non-english Del: in docs -- https://master.php.net/note/delete/117224/in+docs Del: other reasons-- https://master.php.net/note/delete/117224 Reject -- https://master.php.net/note/reject/117224 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202445) next »