Friday, November 11, 2022

Foreach loop for Beginner Guide

 Foreach loop for Beginner Guide






The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

// index array using foreach loop

$num_list = [10, 20, 30, 40, 50, 60, 85];

foreach ($num_list as $number) {
echo $number . "<br>";
}
echo "<br>";
// Associative array using foreach loop.

$age = array("Peter" => "35", "Ben" => "37", "Joe" => "43");
foreach ($age as $name => $value) {
echo "$name = $value" . "<br>";
}

0 comments

Post a Comment