Why does PHP foreach want to copy an array out of action

Source: Internet
Author: User
Tags php foreach
What is the purpose of seeking the process

Reply content:

What is the purpose of seeking the process

What do you mean? Forgive me for being stupid, what do you mean?

PHP foreach does not need to copy an array out of operation, where do you see the need to copy an array?

The PHP foreach traversal is a copy of the array, and I think it's probably to prevent you from traversing one side while modifying (adding items) to cause a dead loop?
A random guess ...

The main idea is to know foreach when an internal implementation is a copy of 临时变量 an operation instead of manipulating the actual array.

I have been very ignorant of this problem, the following answer is for reference only, ah haha

Refer to In-depth understanding of the PHP kernel

To look at a column sub foreach -proof in the loop is really a copy of a 临时变量 to operate

$arr = array(1,2,3,4,5);//该数组的下一元素为 2echo next($arr);//2//该数组的最后元素为 5echo end($arr);//5//即使上一步调用end将数组指针移到最后一位, 但在foreach开始执行时, 会重置数组指针foreach($arr as $key => $row) {    next($arr);    end($arr);    echo $key, '=>', $row, "\r\n";//}

The results are as follows

250=>11=>22=>33=>44=>5

As above, foreach at the beginning of the loop, the call next or end time will change the position of the array pointer, while in the foreach inside call, its loop process is unchanged

In the traversed code, the pointer to an array, such as End,next, does not change, because when the PHP kernel fetches, the internal pointers to the current array of operations are stored through the intermediate variables, each traversing an element, the position of the previously stored pointer is fetched, and the next element is fetched. The key to recovering the pointer position is the intermediate variables during fetch opcode execution.

Here you can see the foreach original array in the loop operation from time to time, but the copy of the original array,

Well now back to the question of the Lord, why copy an array to manipulate instead of manipulating the original array, my idea is, if foreach the operation is the original array, then foreach change the original array in the value, or new elements, then what will be, the main question is not to be able to understand,

foreach ($variable as $key => $value) {    $variable[''] => mt_rand(1, 111);}
  • 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.