Foreach Loop

Foreach loop acts only on arrays and acts similar to For loop. Difference is you don’t have to define an incrementer and increment it. There is also no condition to evaluate. It would run for each item in the array and won’t run at all if the array is empty.

Syntax

foreach (array as value)  {
block of codes
}

Example

<?php
$array = array(1,2,3,4,5);
foreach($array as $value)
echo $value.” t”;
?>

Output

1 2 3 4 5