I heard about a database in a doctorβs surgery that made this very mistake using software. It was tested with about 100 entries, everything worked perfectly. For several months, he dealt with millions of records and was completely unusable, requiring minutes to download results. The code was replaced in accordance with the answers above, and it worked perfectly.
To think about it differently, a fairly powerful dedicated server that does nothing else will take about 1 nanosecond to do the count ($ array). If you had 100 cycles, each of which counted 1000 lines, then only 0.0001 seconds.
However, it is 100,000 calculations to load each page. Scale that up to 1,000,000 users (and who does not want to have 1 million users?) ... does a 10-page download, and now you have calculations for 1,000,000,000,000 (1 trillion). This will lead to a large load on the server. This is the 1000 seconds (about 16.5 minutes) that your processor spends executing this code.
Now increase the time the machine takes to process the code, the number of elements in arrays and the number of loops in the code ... you are talking about literally many trillions of processes and many hours that can be avoided by simply storing the result in a variable.
Dan blows
source share