A pitfall referenced by foreach in PHP

Source: Internet
Author: User
Let's talk about a pitfall referenced by foreach in PHP. let's take a look at the following code:

 $ Val) {// some code} echo $ val; // output dog echo $ key; // output 3 // assign a value to the following val values: $ val = 'e '; print_r ($ arr); // output Array ([0] => apple [1] => banana [2] => cat [3] => dog)

Note: In the preceding foreach loop, $ key and $ val variables are not automatically released after the loop ends. The value is saved. Changing the value of $ val does not affect $ arr.

Reference: If you want to modify the elements of an array during array traversal, you can reference $ val in foreach. The referenced element $ val points to the memory address of the current array element, that is, the shared memory address. Therefore, changing the value of $ val changes the value of $ arr [$ key] at the same time.

Let's take a look at the following section using the referenced code in foreach. this is a situation recently encountered in the project:

 & $ Val) {$ val = 'New Value';} echo $ val; // Output new value echo $ key; // output 3 $ val = 'egg '; print_r ($ arr); // output Array ([0] => new value [1] => new value [2] => new value [3] => egg)

Note: After foreach is used and referenced, $ key and $ val variables are not automatically released after foreach ends, but $ val and $ arrcount ($ arr) -1 points to the same memory address. Therefore, changing the value of $ val also changes the value of $ arr [3.

In this case, it is easy to make a mistake, as shown in the above example, to continue using variables referenced by foreach outside the loop, so that developers will not get the expected data. Therefore, to avoid this situation, release the reference of the variable at the appropriate position. The above code is used as an example:

 & $ Val) {$ val = 'New Value';} unset ($ val); echo $ val; // error, Notice: Undefined variable: val echo $ key; // output 3 $ val = 'egg'; print_r ($ arr ); // output Array ([0] => new value [1] => new value [2] => new value [3] => new value) after foreach ends, unset $ val is released. Therefore, changing $ val will not affect $ arr.

This is a summary of the pitfalls and Solutions recently encountered in the project. if you have any errors or better suggestions, please note.

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.