What happens internally when we use foreach (PHP5)?

Source: Internet
Author: User
All of the following conclusions are based on the PHP5 version

Look at the following section of the most basic foreach traversal array code.

 
  $value) {    echo $key, $value, '
’; Output:

Output for ' 0a1b2c ' naturally no doubt, then the process of $arr, $key, $value exactly what is the operation after the output of this result?

In fact, the foreach traversal process, not directly manipulate $arr (the original array), but will be copied $arr a $arrcopy (is a $arr of a replica, I here with $arrcopy instead), foreach in the process of operation is actually always $ Arrcopy.

Note: About $arrcopy This value we are not able to extract, because this is the name that I gave him, and there is no such variable, but the foreach traversal process does produce such a copy, here for the convenience of describing I use $arrcopy representative.

The approximate process of a foreach traversal is this (pseudo-code):

 
  ’;} Firt loop End//second Loop $key = Currentkey ($arrCopy); Assigns the obtained value to the $k; $val = Currentval ($arrCopy); Assigns the obtained value to the $v;.........//seconde loop end?>

This is the process of the foreach code, summed up in a word that the foreach traversal operation is not the original array, but a copy of the array, but at the end of each loop will be re-assigned copy value back to the original array $arr = $arrCopy;.

How can I prove my claim? It can be checked with the following code.

 
  $value) {    $arr [] = ' d ';    Print_r ($arr);    Var_dump ($key, $value);}? >

The output is:

Output:array    (    [0] = a    [1] = b    [2] = = C    [3] = d    )    int (0)    string (1) "a"    Array    (    [0] = a    [1] = b    [2] = = C    [3] = d    [4] + D    )    int (1)    string (1)  "B"    Array    (    [0] = a    [1] = b    [2] = = C    [3] + D    [4] + D    [5] = = D    )    int (2)    string (1) "C"

Did the students see it?

The key-value pairs of the $arr array have been increasing, but $key, the value of $value to int (2), string (1) "C" ended, and did not print as we expected for those key-value pairs with the value d.

Here you can prove that the foreach traversal is a copy of the $arr ($arrcopy) operation.

By the way, there are a few small areas to be aware of during foreach use. For example, a foreach traversal array pointer problem:

 
 
  $value) {}var_dump (current ($arr)); Output:bool (False)?>

Two times output, different results. Why is it? Because a Foreach loop iterates through an array, the pointer to the array is pointed at the end (the pointer is to the right of ' C '), and is not reset after use, so we var_dump (current ($arr)) to bool (false). So here we need to pay special attention, in order to be safe, after the foreach traversal array, it is best to manually reset () the array to prevent errors:

 
  $value) {}reset ($arr); Var_dump (current ($arr)); /output:string (1) "A"?>

This is normal.

There's also a little PHP manual that reminds us:

The idea of turning into code is:

 
  $value) {}var_dump ($key); Var_dump ($value);? >

After the foreach traversal, the $key and $value are real, and it is best to manually unset () out after use.

Summary: foreach is a more complex PHP inside a function, because the PHP bottom of the structure of the C language, reference (IS_REF__GC), pointer movement ..., so when using foreach must pay special attention AH!

  • Related Article

    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.