Yes, I agree there are some great tools out there for managing multiple sources of data, but sometimes we are limited by the business specifications. I also agree that foreach() looks a lot nicer than a for() loop, but I'm not 100% sure that it is faster than a for() loop with the length calculated in advance. I've seen a lot written about that subject with various benchmarks going both ways. Of course, in most situations, the difference will be negligible either way.
As you can see here : 3v4l.org/7Kvf5 , PHP7 made foreach() 50% as fast as for(). It depend of version tho
To be honest, that's premature optimisation to use for() instead of foreach() from now. If you really need high-performance, you shouldn't use PHP imo
He/Him/His
I'm a Software Engineer and a teacher.
There's no feeling quite like the one you get when you watch someone's eyes light up learning something they didn't know.
Sometimes it's worth mentionning the fact that with low effort, you can improve a lot the process and (because that's the only thing that interest people) the result
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yes, I agree there are some great tools out there for managing multiple sources of data, but sometimes we are limited by the business specifications. I also agree that foreach() looks a lot nicer than a for() loop, but I'm not 100% sure that it is faster than a for() loop with the length calculated in advance. I've seen a lot written about that subject with various benchmarks going both ways. Of course, in most situations, the difference will be negligible either way.
As you can see here : 3v4l.org/7Kvf5 , PHP7 made foreach() 50% as fast as for(). It depend of version tho
To be honest, that's premature optimisation to use for() instead of foreach() from now. If you really need high-performance, you shouldn't use PHP imo
Unfortunately, sometimes those decisions are made for you by others...
Sometimes it's worth mentionning the fact that with low effort, you can improve a lot the process and (because that's the only thing that interest people) the result