Welcome to W3Courses
 Like Us on Facebook

Loop through an Array using Foreach Function in PHP

You can loop through an array using foreach function. So instead of using:

<?php

$names = array("Paul","Maria","David","Lucy","Ana");

$i=0
while($names[$i] == true)
{
     echo("$names[$i]");
     $i++;
}

?>
 


You can be more efficient and use foreach:

<?php

$names = array("Paul","Maria","David","Lucy","Ana");

foreach ($names as $myname)
{
     echo("$myname");
}

?>

Share