The for loop carry out a block of code a specified number of times the script should run.
Syntax:-
for (init counter; counter compression; increment counter) {
Execution code….;
}
Factors:-
init counter: Initialize the loop counter value
counter compression: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value
Example:-
<?php
for ($x = 0; $x <= 10; $x++) {
for ($y=0; $y <= 10; $y++){
echo $x;
}
}
?>
Foreach Loop:-
The foreach loop works with arrays and used key/value pair in an array.
Syntax:-
foreach ($array as $value) {
execution code…;
}
Example:-
<?php
$clr = array(“red”, “white”, “blue”, “yellow”);

foreach ($clr as $value) {
echo “$value <br>”;
}
?>

Categories: PHP

0 thoughts on “PHP For Loops”

Leave a Reply