3 ms·
There are some similar benchmark here: http://www.phpbench.com/ http://www.phpbench.com/ Intuitively the for loop should be a lot fast as it isn't assigning an
by robryan 14y ago
There are some similar benchmark here: http://www.phpbench.com/ http://www.phpbench.com/
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.
- josegonzalez 14y agoIt only assigns $user in a foreach when the $user is modified, or if you are trying to outsmart PHP-core and use & to pass it as a reference.
- encoderer 14y agoI get what you mean, but it might be helpful to translate it for everybody else: 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.