Php For Loops

PHP for loop can be used to traverse set of code for the specified number of times.

Syntax :-

Example :-

Result

1
2
3
4
5
6
7
8
9
10

PHP Nested For Loop

We can use for loop inside for loop in PHP, it is known as nested for loop.In case of inner or nested for loop, nested for loop is executed fully for one outer for loop. If outer for loop is to be executed for 3 times and inner for loop for 3 times, inner for loop will be executed 9 times.

Example :-

Result

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

For Each Loop

PHP for each loop is used to traverse array elements.

Syntax :-

Example :-

Result

Red
green
blue
yellow