Tuesday, 15 August 2017

How to Use For and foreach Loop In Php

The PHP for Loop

Syntax
for (init counter; test counter; increment counter) {
    code to be executed;
}

Example
<?php
for ($x = 0; $x <= 10; $x++) {
    echo "The number is: $x <br>";
}
?>

The PHP foreach Loop

Syntax
foreach ($array as $value) {
    code to be executed;
}

Example
<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
    echo "$value <br>";
}
?>

Post a Comment

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search