Home  >  Article  >  Backend Development  >  Analysis of the difference between for and foreach in PHP_PHP Tutorial

Analysis of the difference between for and foreach in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:31:01918browse

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 suppress error messages with "@".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323085.htmlTechArticleNote: 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, for the returned array...
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