CODING ACADEMY

View Original

How to use FOR Loops in PHP

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:

See this content in the original post

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:

See this content in the original post

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.