PHP leaves the array reference problem after the Foreach loop

Source: Internet
Author: User

eg


Result


Explain:

1.foreach is not a block-level field, array reference ends, $k, $a does not free memory

2. When the first reference loop ends, the $a points to the last element of the array 4

3. When the second start loop (at which point $a is already pointing to the last element, is the reference)

The variation of the cyclic 4-time array is:

1-> [1,2,3,4]

2-> [1,2,3,4]

3-> [1,2,3,3] The third value points to the last element 3 but value is the global reference when the last loop points to the final element so value=3 also changes the value of the last element to 3.

4-> [1,2,3,3]



Precautions:

The 1.foreach implementation principle has an internal pointer concept

2.& 's foreach Loop end pointer points to the last element


How to avoid:

After using &foreach cycle unset ($a)

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.