PHP5 and PHP7 foreach cycle problem

The

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:

clipboard.png

PHP5:

clipboard.png

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?

Php
Mar.04,2021

after studying all night, I probably learned something. In general, PHP 5 copies the array as it traverses through foreach values. But PHP 7 internal implementation of this iterative array is different from PHP5. PHP 7 does not rely on array internal pointers, while PHP5 relies on internal pointers. Verify that PHP 5 copied the array under foreach

$arr = [0];
foreach ($arr as $k => $v) {
    debug_zval_dump($arr);
}

the printed refcount is 3, indicating that the array is copied in foreach, resulting in a refcount of 3. Further verification.

$arr = [0];
foreach ($arr as $v) {
    $copy = $arr;
    debug_zval_dump($arr);
}

assume that the array is copied in a loop, then the refcount should be 4. The print result is the same as my guess. Indicates that the array is copied in foreach. And is not affected by the length of the array. Because when the length of the array is 2, it still prints 4. In PHP5 foreach, the array pointer moves to reach the value of the iterative array.

$arr = [0, 1];
foreach ($arr as $v) {
    $copy = $arr;
    debug_zval_dump($arr);
}

how foreach works

in-depth understanding of PHP principle variable separation / reference (Variables Separation)

what happens internally when we use foreach (PHP5)?

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38ae9-2c140.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38ae9-2c140.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?