PHP Tidbit: Efficient Loops
Posted by Jon Lee in Efficiency, Web Development, tags: Efficiency, looping, PHP, programming, tidbit, tipsLoops can save a lot of redundancy in your code by performing a particular task over and over again. However, to keep the loop as efficient as possible, here are some tips to follow. Note that these tips apply to many programming languages, not just PHP but examples are given in PHP code.
1) Avoid nested loops if possible.
Nesting loops is sometimes necessary but if possible, it’s best to avoid them. Each nested loop exponentially increases the number of iterations required.2) Avoid functions in loop heads
Here is a very common inefficiency that I see. It goes something like this:
for ($i=0; $i<count($myarray); $i++) {
//do something
}
using the count() function in the header of your loop requires the array to be counted every interaction. Instead, do the count once, store it in a variable and use that instead!
$numelements = count($myarray);
for ($i=0; $i<$numelements; $i++) {
//do something
}
3) Don’t go through the same loop twice
If you’re pulling data from an array and displaying it in two places on your page, there is no need to go through the loop twice! Simply save that data into a variable the first time around and call that variable the second time.
If your application is relatively simple you probably won’t notice any difference at all after applying these tips. Sometimes I feel we’re spoiled by the sheer amount of computing power we have available to us that we don’t care about coding efficiently. When I do statistical programming (looping over millions of elements), these simple tips actually do help shave minutes off running time!
Popularity: 3% [?]
















Entries (RSS)
I totally agree with the last para. The fact is that, earlier, I used to feel what we would be achieving by doing some simple tweaks to the code. But I realised later that programming isnt all of 20 lines and such tweaks can go a long way in efficient execution
I saw a set of benchmark results for PHP somewhere, and saving the result of a count in a variable made a huge difference relatively in terms of performance. Also, the foreach loop is the most efficient way (even if only slightly compared to some others) to go through an array.
very clearly explained, great post
I would definitely blog about it. Right now I’m learning PHP so this Loops are very useful for me.
I don’t use the foreach loop nearly enough.
I’m too old school with my manual looping
I’m guilty of lazy programming myself sometimes. I mean if I were to simply whip up a little script to add up a small multi dimensional array, I’d probably just nest a lot of loops. (Instead of using an efficient algorithm or looking up a built in function)
How do loops improve programming efficiency?
Not loops improving programming efficiency, but how to program loops efficiently when you need to use them.
I am taking courses at DeVry in class we will be discussing “How do loops improve programming efficiency?” I was hoping for your opinion either for or against. I would really like to hear your views on this subject.
Thank You,
Amy
On the most basic level, loops are used to repeat lines of code that would otherwise be repeated manually many times or even infinitely.
Also, they can be used to repeat a line of code with slightly different parameters each time.
For example, if you want to output the numbers 1 to 100, you could use something like this (depending on programming language):
echo 1;
echo 2;
echo 3;
echo 4;
… etc… until echo 100;
obviously this is inefficient and time consuming (and if you want to count infinitely, it’s also impossible!)
So instead you would use a loop:
$number = 0
while ($number < 100) {
echo $number;
$number = $number + 1;
}
This way you can easily change what number to display up to or even display infinitely many numbers.
Loops are one of the basic constructs of programming and in fact I’d say almost every single program out there employs at least some type of looping.
Hope that helps!
thank you
Very nice
btw very good tips i will use some of them. Some i did not knew.
foreach loops are NOT more efficient than for loops (with numeric indexes)
foreach will create a copy of the array you are looping through, assigning variables along the way (ie; foreach($copycreated AS $key => $value)) whereas the for loop simply asks the array for the key/value it needs.
But the problem with for loops is that you need the entire array read into memory, whereas foreach reads an array (from a file) line by line.
If written correctly, using foreach instead of for can save a low-end machine’s memory from being consumed each time the code is executed.
It becomes very important when using multi-dimensional arrays with a count() of over 100,000.