We all know that in a normal for
-loop you should extract the count()
out of the loopheader to achieve a better performance, meaning you should use
$size = count($array);
for($i; $i < $size; $i++) {...}
instead of
for($i; $i < count($array); $i++) {...}
to avoid the count()
call in every repeat.
But how does foreach
works in this case? I'm quite sure that overall it's slower than a for
-loop because of the as $key => $value
expression handling but that's not what I'm thinking about. I'm interested in the implementation of the "counting" part of the foreach
-loop.
Does anyone knows?
Edit: After this question was flaged as a possible duplicate of this question: I don't want to know about the handling of the array cursor, but of the implementation of the counting part.