by nodesocket · 14y ago · 0 comments
for ($i = 0; $i < count($users); $i++)
vs foreach($users as $user)Intuitively the for loop should be a lot fast as it isn't assigning anything each loop. Although in your example it may be slower because the count($users) would be ran on each iteration of the loop.
Foreach is doing an assignment in each iteration, robryan is right. But PHP variables are copy-on-write so $user is, until modified, just an entry in the symbol table which is very fast.
And that for() loop also has an assignment -- the incrementer.
Fix that:
for($i = 0; $count = count($elements); $i < $count; $i++) { }