Pointers For Newbies, Reminders For Oldies

From: Date: Wed, 27 Jan 2010 15:42:20 +0000
Subject: Pointers For Newbies, Reminders For Oldies
Groups: php.general 
Request: Send a blank email to php-general+get-301552@lists.php.net to get a copy of this message
"... should be obvious - but are often overlooked - points within coding practice that can cause the programmer to develop bad habits and bad code." - Dan Brown Tip #1: Don't use count() in loops unless there are very few items to count and performance doesn't matter, or the number will vary over the loop. That is, don't do this: for ($i = 0; $i < count($items); $i++) Instead, do this: $number = count($items); for ($i = 0; $i < $number; $i++) Reason: when you use the count() call at the top of the loop, it will re-evaluate the number of items each time it's called, which usually isn't necessary and adds time. Instead, work out the number of items before going into the loop and simply refer to that for the number of items in controlling the loop. Paul -- Paul M. Foster

« previous php.general (#301552) next »