Syntax
Syntax of foreach in php is as follows:
foreach ($array as $value)
{
code to be executed;
}
Where “$array” is an array variable and “$value” will contain the each element of this array one by one in each iteration.
Example
I am using a very simple example to demonstrate the use of foreach in php:
< ?php
$myarray=array(‘Bill’,’John’,’Thomas’,’Bred’,’Tom’);
foreach ($myarray as $names)
{
echo $names.”, ”;
}
?>
The output of above code will be “Bill, John, Thomas, Bred, Tom, ”.
This was a very simple example to help you understand how foreach works. For more help and explanation please feel free to comment.
Reference:
www.w3schools.com/
No comments:
Post a Comment