For & Foreach loop in PHP – website development

In this video

This tutorial is about For & Foreach loop in PHP. In our last lecture we learned While & Do While Loop PHP. If you haven’t watched it yet then here is the link of tutorial .

 For & Foreach loop in PHP

In this tutorial we will learn about For & Foreach loop in PHP.


For loop statement is the most complex loop statement in PHP. The for loop is used when you already know how many times the code should run, so here is code.

for (initialization; condition; increment)


code here;


The initializer is used to set the start value for the counter of the number of loop iterations.


for ($x = 0; $x <= 5; $x++) {

    echo  $x ;



Output: 012345

Foreach Loop SYNTAX

Foreach loop statement in PHP allows you to iterate over elements of an array . The foreach loop statement only works with arrays and objects, so here is code.

foreach ($array as $value) {

code here;


For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.


$colors = array(“rose”, “garden”, “ball”, “bat”);

foreach ($colors as $value) {

    echo “$value <br>”;



Output: rose

For & Foreach loop in PHP tutorial end here. In next tutorial we will learn about switch case in php. For queries ask us on our Facebook page link is given below, Thank you for watching us.


Tutorial Hive’s mission is to provide quality education to all our community. Tutorial Hive’s mission is to provide free education to all those who cannot afford BIG FEE of institutes. We aim specifically to empower youth to understand programming related knowledge which further helps the society to reduce complex problems through applications. Why Tutorial Hive? We not only provide the theoretical knowledge but also provide the actual implementation with working sample applications. Tutorial Hive cannot achieve its mission without the help of our community. So, please if you think you can help in any manner feel free to contact us. Or if you see any mistake in our content which can be modified/corrected then please leave us a message

We are always open for suggestions.


Subscribe on YouTube:

Follow us on Social Media

You might be interested in


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