PHP Tutorial
The foreach loop is used to traverse the array elements. It works only on array and object.
The foreach loop is used to loop through each key/value pair in an array.
In foreach loop, we don't need to increment the value.
foreach ($array as $value) {
code to be executed;
}
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
Let's put these syntax into real use.
<?php
$number = array("10", "11", "12", "13","14","15");
foreach ($number as $value) {
echo "Number = $value <br>";
}
?>
This will produce the following result −
Number = 10
Number = 11
Number = 12
Number = 13
Number = 14
Number = 15