How to use FOR Loops in PHP

 
phplogo.jpg

In this lesson I will show you how to use FOR loops in PHP. After starting off with a basic loop to print out the first 10 counting numbers, then extending into decreasing values and listing multiples, I will move onto finally showing you how to create a loop within a loop and print off the multiplication tables.

The format of the FOR loop is as follows:

So, inside the round brackets we must list 3 things:

  • initialise the counter: in other words, what value do we start from

  • test the counter: this is usually the value we are going to

  • increment the counter: we usually increase the counter by 1 but it can also be incremented by another value or even decremented

A typical FOR loop may look something like this:

The example above will print out the numbers 1 to 10.

The video below explains some loops in more detail and will also show you how to create a loop within a loop.