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!

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


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

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
