For loop PHP

By | October 12, 2022

The FOR loop is a special construct that allows you to repeat blocks of code a certain number of times, without having to write out the whole thing each time. This is useful for iterating over collections, such as arrays, in order to perform some action.

It can be used when we know the how many times code will be repeated. FOR loops are executed from left to right (as opposed to WHILE loops which are executed from top to bottom). They also use an incrementing variable instead of a counter variable (which makes them more like a while loop than a while loop).

Syntax

for (initial number; condition; increment number) {
  statement to be executed;
}