Introduction to Loops in PHP
In this lesson, we will begin studying loops. Loops are used to make a certain section of code execute several times in a row.
Why is this needed - imagine that you need
to square 100 array elements.
If you access each element separately
by its key - it will take 100 lines
of code, and writing this code
will take quite a lot of time.
But this is not necessary - we have the ability to make PHP perform a certain operation the required number of times for us. For example, square all the elements of an array. This is done using loops.
Loops can repeat a certain code a specified number of times. Each such pass through the loop is called a iteration of the loop.
Loops often use special
variables that each iteration
increase their value by one.
Such variables are called loop counters.
Counters are used to
count how many times the loop has executed.
For counters, it is customary to use the letters
i, j and k.
In the next lessons, we will begin studying loops in PHP.