It is used when one knows how many time one need to run the particular loop. It is also of two types:
1. PHP for loop statement
Loop back again and again through specified time duration.
Read More
PHP for loop syntax:
1 2 3 | for (initialization; condition check; increment/ decrement counter) { code to be executed; } |
Example: PHP for loop example
1 2 3 4 5 | <?php for ($a = 0; $a <= 4; $a++) { echo "The number is: $a <br/>"; } ?> |
Result:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
2. PHP foreach loop statement
For each elements in the array it give result.
It is used to iterate all the elements over array.
PHP foreach loop syntax:
1 2 3 | foreach ($array as $value) { code to be executed; } |
Example: PHP foreach loop example
1 2 3 4 5 6 | <?php $Taxi = array("swift", "alto", "toyoto", "I10"); foreach ($Taxi as $value) { echo "$value <br/>"; } ?> |
Result:
swift
alto
toyoto
I10
Reference