What are the differences between the for
loop and the foreach
loop in PHP?
It should be pretty simple
foreach
abstracts away some of the complexity and is usually easier. I use this whenever I don't need to know the numerical index of the array or $key => $value
won't provide me with it.
for
is the older C style where you must first perform a count()
so you know how many iterations the loop requires. It is useful when you need to know the index, or to count backwards or step through in different groups.