Home >Backend Development >PHP Tutorial >Analysis of the difference between for and foreach in fourleafclover PHP

Analysis of the difference between for and foreach in fourleafclover PHP

WBOY
WBOYOriginal
2016-07-29 08:44:331052browse

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)


Note: foreach does not support the ability to use "@" to suppress error messages.

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn