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:
1 2 3 4 5 6 7 8 |
|
instead of being more intuitive and direct:
1 2 3 |
|
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.
1 2 3 4 5 6 7 |
|
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).
1 2 3 4 5 6 7 |
|
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).
1 2 3 4 5 6 7 |
|
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
