Note: Unless the array is referenced, foreach operates on a copy of the specified array, not the array itself. Therefore, the array pointer will not be changed by the each() structure, and modifications to the returned array cells will not affect the original array.
1. Since php5, foreach may also traverse the properties of the object.
2. Since php5, foreach can easily modify the cells of the array by adding & before $value. This method will assign the value by reference instead of copying a value.
Copy code The code is as follows:
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
?>
Output: $arr=array(2, 4, 6, 8)
The above introduces the analysis of the difference between for and foreach in fourleafclover PHP, including the content of fourleafclover. I hope it will be helpful to friends who are interested in PHP tutorials.