PHP Loops | foreach

Hi everyone,

This loop type is used with arrays. It goes through the end of an array and runs the loop for each array value. The syntax is :

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

Here is an example about foreach loop:

output will be :

facebook
twitter
google+

<?php
$social = array(
    "facebook",
    "twitter",
    "google+"
);
foreach ($social as $value) {
    echo $value . "</br>";
}
?>

For each loop iteration, the value of the current array element is assigned to $value. And the array pointer is moved by one. So the next time of the loop you will be looking at the next array value. 

In this lesson, you’ve learned the foreach loop in PHP. Stay cool and stay connected to learn more! 

Leave a Reply

Your email address will not be published. Required fields are marked *