PHP For Loop
PHP For loop executes a square of code a predefined number of times.
For Loop in PHP
The for loop is utilized when you know ahead of time how often the content should run.
Syntax
for (init counter; test counter; increment counter) {
code to be executed;
}
Parameters:
init counter: Initialize the loop counter esteem.
test counter: Evaluated for each loop cycle. In the event that it assesses to TRUE, the circle proceeds. On the off chance that it assesses to FALSE, the circle closes.
addition counter: Increases the loop counter esteem.
Example:
<?php
for ($x = 0; $x <= 10; $x++) {
echo “The number is: $x <br>”;
}
?>
PHP For Each Loop
The for each loop works just on exhibits, and is utilized to circle through each key/esteem pair in a array.
PHP For Each Loop Syntax:
foreach ($array as $value) {
code to be executed;
}
For each loop emphasis, the estimation of the present array component is allocated to $value and the array pointer is moved by one until it arrives at the last exhibit component.
Example:
<?php
$colors = array(“red”, “green”, “blue”, “yellow”);
foreach ($colors as $value) {
echo “$value <br>”;
}
?>
See More Examples: PHP For Loop