Re: Pointers For Newbies, Reminders For Oldies

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2010-01-27 at 08:01 -0800, Michael A. Peters wrote:

> Paul M Foster wrote:
> > "... 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++)
> 
> Gah!
> 
> for ($i=0;$i<sizeof($array);$i++)
> 
> is something I do all the time.
> So the array size is being calculated each iteration?
> 


Yeah, the condition is evaluated once for each cycle of the loop. I use
the count() a lot myself tbh, but I don't often have the call to work
with massive data sets and huge loops.

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux