Home of web learners

www.webn3rd.com

Foreach loop is special loop for using loop in array which built in for array

Its structure like for loop

Lets look an example

<?php
$color = array("green", "blue", "red");
foreach ($color as $value) {
echo $value."<br/>";
}
?>

output:

green

blue

red

Here,we see that iniialization part comes with array or you can use other array for initialization,which used inside foreach loop as $color

Here first variable in foreach loop need to be array as here $color

Then in which variable need to store array single value have to declare as here $value

Here no condition used as it iteration goes upto the last data in array,as here goes to 3 times as total array data is 3

You don't have to use increment or decrement as it done auto,one increment to go next data index

Then inside body of loop,you can use variable where data is assigned in loop,here $value

Here we show $value data,so its output all data inside array

Fact is that as it go for first data of array,so here we use br to go next line for new data,so for each time iteration you can work with one data in body part

webn3rd.com

About webn3rd