Home » Php » php – Why count is bad than $count

php – Why count is bad than $count

Posted by: admin July 12, 2020 Leave a comment


I was just reviewing the answers to different questions to learn more. I saw an answer which says that it is bad practice in php to write


It says that calling the count function in the loop reduces the speed of the code. The discussion in the comments on this question was not clear. I want to know why it is not good practice. What should be the alternative way of doing this?

How to&Answers:

You should do this instead:

$count = count($array);

The reason for doing this is because if you put the count($array) inside the for loop then the count function would have to be called for every iteration which slows down speed.

However, if you put the count into a variable, it is a static number that won’t have to be recalculated every time.


For every iteration, PHP is checking that part of the loop (the condition) to see if it should keep looping, and every time it checks it, it is calculating the length of the array.

An easy way to cache that value is…

for($i=0,$count=count($array);$i<$count;$i++) { ... }

It is probably not necessary in small loops, but could make a huge difference when iterating over thousands of items, and dependent on what function call is in the condition (and how it determines its return value).

This is also why you should use foreach() { ... } if you can, it uses an iterator on a copy of the array to loop over the set and you don’t have to worry about caching the condition of the loop.


I heard of a database in a doctor’s surgery that made exactly this mistake with a piece of software. It was tested with about 100 records, all worked fine. Within a few months, it was dealing with millions of records and was totally unusable, taking minutes to load results. The code was replaced as per the answers above, and it worked perfectly.

To think about it another way, a fairly powerful dedicated server that’s not doing much else will take about 1 nanosecond to do count($array). If you had 100 for loops, each counting 1,000 rows then that’s only 0.0001 of a second.

However, that’s 100,000 calculations for EVERY page load. Scale that up to 1,000,000 users (and who doesn’t want to have 1 million users?)… doing a 10 page loads and now you have 1,000,000,000,000 (1 trillion) calculations. That’s going to put a lot of load on the server. It’s a 1,000 seconds (about 16.5 minutes) that your processor spends running that code.

Now increase the time it takes the machine to process the code, the number of items in the arrays, and the number of for loops in the code… you’re talking of literally many trillions of processes and many hours of processing time that can be avoided by just storing the result in a variable first.


It is not good practice because as written, count($array) will be called each time through the loop. Assuming you won’t be changing the size of the array within the loop (which itself would be a horrible idea), this function will always return the same value, and calling it repeatedly is redundant.

For short loops, the difference probably won’t be noticeable, but it’s still best to call the function once and use the computed value in the loop.