Foreach Loops in PHP with example

In PHP, foreach loops execute a block of program a specified number of times, or while a specified condition is true.
Syntax :
foreach (array_expression as $value)
{
    Statement(s)
}

Syntax with Key:
foreach (array_expression as $key => $value)
{
    Statement(s)
}

Print some numbers from 1 to 4 using For loops
<?php
$userAges = array();
$userAges[“user01”] = “27”;
$userAges[“user02”] = “25”;
$userAges[“user03”] = “22”;
$userAges[“user04”] = “25”;
$userAges[“user05”] = “24”;
foreach ($userAges as $age) {
    echo $age;
}
?>

Output : 27 25 22 25 24 .

Print some numbers from 1 to 4 using For loops
<?php
$userAges = array();
$userAges[“user01”] = “27”;
$userAges[“user02”] = “25”;
$userAges[“user03”] = “22”;
$userAges[“user04”] = “25”;
$userAges[“user05”] = “24”;
foreach ($userAges as $name => $$age) {
    echo $name.”-“.$age.”
“;
}
?>

Output :
    user01-27
    user02-25
    user03-22
    user04-25
    user05-24 .