| |
Output

Foreach Loop
The foreach loop works only on arrays. It loops through a block of code for each element in an array. On each loop the value of the current element in array is assigned to $value and the array pointer is advanced by one. Similarly in the next loop the value of the next element in array is assigned to $value and the array pointer is advanced by one. This process is repeated till the end of the array.
| Syntax |
foreach (array as value)
{
code to be executed;
} |
| |
In the following example the values of the given array will be printed by the loop.
Exampl |
<html> |
<body> |
|
<?php |
$arr=array("hello", "how", "are", "you"); |
foreach ($arr as $value) |
{ |
echo "The value is: " . $value . "<br />"; |
} |
?> |
|
</body> |
</html> |
Output
|