There are two kind of for loops in PHP, one is for() loop & other is foreach().
The for() loop has three sections: init, termination & increment.
Syntax:
for (initialization; Condition Check ;increment) {
code to be executed;
}
First section initializes the loop, Second section is the gate keeper that check the condition, if it returns true, we run the statements in the loop, if it returns false, we exit the loop.
The example below displays the numbers from 0 to 5:
<?php
for ($x = 0; $x <= 5; $x++) {
echo "The number is: $x <br>";
}
?>
Output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
So in the case we have just seen, the loop will run 6 times
PHP5 foreach Loop
The foreach loop works only on arrays that match each key/value pair in an array through loop.
Example
<?php
$website = array("Welcome", "to", "FindNerd");
foreach ($website as $value) {
echo "$value ";
}
?>
output: Welcome to FindNerd
0 Comment(s)