In php foreach loops are used to access all the elements of the array. The syntax is as
foreach ($array as $key =>$value)
{
}
In above syntax , the no. of times loop get executed is equal to the no of elements in the array.
the same above syntax can be written as
foreach ($array as $value) {
}
In the above code the value of the array gets assigned to the variable $value every time the loop gets executed.
foreach loop always works on the array only with the key-value pair.
example:
<?php
$days = array("Monday", "Tuesday", "Wednesday","Thursday","Friday","Saturday", "Sunday" );
foreach ($days as $value) {
echo "$value <br>";
}
?>
output:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
0 Comment(s)