In-depth understanding of PHP foreach Mechanism

Source: Internet
Author: User
Tags php foreach
In PHP, if the is_ref value of the foreach array is 1, the original array is directly loop. If foreach ($ arras & amp; $ v) {} finally sets is_ref of $ arr to 1 and directly loops through the original array. If the is_ref of the array is 0 but the reference count is greater than 1, the array is copied and new memory space is opened, loop is... foreach in PHP
If the is_ref value of the array is 1, the original array is directly recycled.
If foreach ($ arr as & $ v) {} finally sets is_ref of $ arr to 1 and directly loops through the original array
If the is_ref value of the array is 0 but the reference count is greater than 1, the array will be copied, and a new memory space will be opened, and the array to be copied cyclically
Is there any problem with the above?
There is another tangled problem.
At the beginning of foreach, the array reference count is + 1. When the array is changed, $ arr [$ k] = 111 or run functions such as current, copy on write?
At the beginning of foreach, the reference count of the array was not + 1, but determined that the array was changed ($ arr [$ k] = 111, run current and other functions) to create new memory space.
Of course, no matter how you do it, the execution results for the Code seem to be the same
However, we are prepared to sort out documents and share them with you so as not to mislead you.
Thank you!

Reply content:

Foreach in PHP
If the is_ref value of the array is 1, the original array is directly recycled.
If foreach ($ arr as & $ v) {} finally sets is_ref of $ arr to 1 and directly loops through the original array
If the is_ref value of the array is 0 but the reference count is greater than 1, the array will be copied, and a new memory space will be opened, and the array to be copied cyclically
Is there any problem with the above?
There is another tangled problem.
At the beginning of foreach, the array reference count is + 1. When the array is changed, $ arr [$ k] = 111 or run functions such as current, copy on write?
At the beginning of foreach, the reference count of the array was not + 1, but determined that the array was changed ($ arr [$ k] = 111, run current and other functions) to create new memory space.
Of course, no matter how you do it, the execution results for the Code seem to be the same
However, we are prepared to sort out documents and share them with you so as not to mislead you.
Thank you!

Solution: http://segmentfault.com/a/1190000004340640

You are so confused ...... The problem is hard to find ......

Is is_ref defined in zval?

The two questions found are related to zval's copy on write. I will give a simple answer to the copy on write mechanism. (This is based on PHP 5, and the zval structure in PHP 7 changes a lot. Let's talk about it separately)

Adding refcount to the push variable indicates that more variables use this value, for example, the array in foreach. Adding refcount to 2 indicates zval in the value. the HashTable in value is used in two variables, and the replication does not happen immediately. However, when modifying a variable, if refcount> 1 (indicating that another variable references this HashTable) is detected, You need to copy the HashTable and modify it.

It should be specified that version 7 has been changed. In order to avoid the trouble, I try to use array walk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.