PHP loop Condition

PHP loop Condition

PHP loop Condition, it can be used to traverse the set of code for the specified number of times and The loopfor is used when you know in advance that how many times the script should run. It should be used if a number of iteration is known otherwise used while loop Condition.
For loop Condition and loop Condition in PHP Programming image

Syntax

PHP loop Condition: Flowchart

PHP loop Condition image

Example-

Finally Output-

PHP Nested For Loop

For loop Condition and loop Condition in PHP Programming, We can also use for loop inside for loop in PHP, it is called 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 (3 times for 1st outer loop, 3 times for 2nd outer loop and 3 times for 3rd outer loop).

Example-

Output-

PHP For Each Loop-

The loopfor each works only on arrays and is used to loop through each key/value pair in an array or PHP for each loop is used to traverse array elements.

Syntax-

For every loop iteration in a foreach loop, the value of the current array element is assigned to $value and the array pointer is moved by one until it reaches the last array element.

The following example demonstrates a loop that will output the values of the given array ($colors):

Example-

Finally Output-

 

Resource
PHP