A foreach loop is a variant for loop and allows you to traverse the elements in an array. There are two different versions of the
foreach Loop. The syntax for the Foreach loop is as follows:
foreach (array as value)
{
code to is executed;
}
foreach (array as key => value)
{
code to is executed;
}
The following example shows the Foreach loop, which prints the value of the given array:
<?php
$email = Array (' john.smith@example.com ', ' alex@example.com ');
foreach ($email as $value) {
echo "Processing". $value. " <br/> ";
}
?>
PHP executes each element in dollars, in turn the email loop body once, the dollar value is set to the current element. Elements
To deal with its internal order. Loop continues until the last element of the Foreach loop or the given array is approximately
Beam.
<?php
$person = Array (' name ' => ' Andrew ', ' age ' =>, ' address ' => '), Lincoln
St. ');
foreach ($person as $key => $value) {
echo $key. "is". $value. " <br/> ";
}
?>
In this case, the key to each element is placed in the $ key and the corresponding value is $ attached.
In foreach constructs the non-business array itself, but it is a copy of it. In each loop, the value of the variable $ value can be
To operate, but the original value of the array remains unchanged.