Home >Backend Development >PHP Tutorial >When does PHP foreach copy?
This article describes the copy behavior of foreach in PHP 5. Requires some knowledge of PHP inner workings, namely zvals, refcount and copy-on-write behaviour.
#PHP’s foreach is a very neat and to-the-point language construct. Still some people don't like to use it because they think it is slow. One reason for the common naming is that foreach copies the array it iterates.
Therefore, some people suggest writing:
$keys = array_keys($array); $size = count($array); for ($i = 0; $i < $size; $i++) { $key = $keys[$i]; $value = $array[$key]; // ... }
instead of being more intuitive and direct:
foreach ($array as $key => $value) { // ... }
There are two problems here:
Microoptimization is bad. Often it just wastes your time and doesn't lead to any measurable performance improvements.
The copying behavior of foreach is a little more complicated than most people think. Typically, the "optimized" version will be slower than the original version.
When does foreach copy?
Whether foreach copies the array and the number of copies depends on three things:
Whether the iteration array is referenced, it How high is the refcount and whether iteration is done by reference.
No reference, refcount == 1
In the code below, $array is not referenced, and refcount is 1. In this case, foreach does not copy the array (proof) - contrary to the popular belief that foreach always copies the iterated array without a reference.
test(); function test() { $array = range(0, 100000); foreach ($array as $key => $value) { // ... } }
The reason is simple: why do this? The only place where foreach modifies $array is that it is an internal array pointer. This is expected behavior, so no prevention is needed.
Unreferenced, refcount > 1
The code below looks very similar to the previous code. The only difference is that arrays are now passed as parameters. This may seem like an insignificant difference, but it does change the behavior of foreach:
It will now copy the array structure, not the value (proof; if you're wondering this is just the copied structure, compare this and that script. The first copies only the structure, the second copies both).
$array = range(0, 100000); test($array); function test($array) { foreach ($array as $key => $value) { // ... } }
This may seem a bit strange at first glance:
Why does an array copy when it is passed as a parameter, but not if it is defined in a function? The reason is that the array zval is now shared between multiple variables: the $array variable outside the function and the $array variable inside the function. If foreach iterates the array without copying the array structure, then it will not only change the array pointer of the $array variable in the function, but also the pointer of the $array variable outside the function. So foreach needs to copy the array structure (i.e. hash table). On the other hand, the values can still share zvals, so no copying is required.
Quote
The next situation is very similar to the previous one. The only difference is that arrays are passed by reference. In this case the array will not be copied (proof).
$array = range(0, 100000); test($array); function test(&$array) { foreach ($array as $key => $value) { // ... } }
In this case, the same reasoning applies to the previous case: the outer $array and the inner $array share zvals. The difference is that they are now references (isref == 1), so in this case any changes to the inner array will be made to the outer array. So if the array pointer of the inner array changes, the array pointer of the outer array should also change. This is why foreach does not require copying.
Iterate by reference
The above examples all iterate by value. For reference iteration, the same rules apply, but appending a value reference changes the copy behavior of array values (the behavior with respect to structure copy remains the same).
The case "unreferenced, refcount == 1" has not changed. Reference iteration means that if there is any change in the $ value, we want to mutate the original array so that the array is not copied (proof).
The "referenced" case also remains the same, in which case a change to $value should change all variables referencing the iterated array (proof).
Only the "unreferenced, refcount > 1" case has changed, because now the array structure and its values need to be copied. array structure, because otherwise the array pointer of the $array variable outside the function would change, and changes to $value would also change the outside $array value (proof).
Summary
If and only if the iterated array is not referenced and has refcount > 1, foreach will copy the array structure
foreach will also Copies an array value if and only if the previous point applies and iteration is done by reference
The above is the detailed content of When does PHP foreach copy?. For more information, please follow other related articles on the PHP Chinese website!