PHP7 upgrade manual mentions that "when foreach traverses through values, the value of the operation is a copy of the array. When traversing the array by default, foreach actually operates on the iterative copy of the array, not the array itself. This means that the operation in foreach does not modify the value of the original array." I thought PHP5"s foreach loop was the array itself, so I verified that the result was not what I thought. But when I looked up the information, I saw a code, which made me feel confused. Please answer my doubts
$array = array("a", "b", "c");
$ref = &$array;
foreach ($array as $val) {
$array[3] = "d";
print_r($array);
echo ": " . $val . "\n";
}
result of PHP7 output:
PHP5:
just refer to the array, why is the result different? Excluding references, the results of PHP7 and PHP5 are the same. Is the foreach loop operation array in PHP5 the array itself or a copy of the array?