Talking about the pitfalls of using referenced variables in foreach in PHP

Source: Internet
Author: User
The following small series will bring you an example of PHP that does not use recursive infinity classification. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at PHP for many years, but we will still make low-level mistakes. today we encounter a pitfall when variables are referenced in foreach. The PHP version is 5.6.12 and the code is as follows:

 &$a) {  $a = $a.'_'. $a;  echo $a .'
';}echo '';foreach ($arr as $i=>$a) { echo $a .'
';}echo '';print_r($arr);

Output result

The result of the second foreach output at the beginning is very inexplicable. how can we output two d_d statements?

I thought about it carefully. because the $ a scope in PHP foreach is the local variable of the entire function, it is still valid outside the loop, rather than being closed in the loop,

Therefore, when executing the second foreach, $ a is not a new variable, but still pointing to the address reference of the 5th elements in the $ arr array,

When the second foreach is in a loop, it is actually constantly assigning values to the 5th elements of the $ arr array,

Specific assignment information,

The first time: a_a is assigned to 5th elements. The result is [a_a, B _ B, c_c, d_d, a_a].

The second time: B _ B is assigned to 5th elements. The result is [a_a, B _ B, c_c, d_d, B _ B].

The third time: c_c is assigned to 5th elements. The result is [a_a, B _ B, c_c, d_d, c_c].

The fourth time: d_d is assigned to 5th elements. The result is [a_a, B _ B, c_c, d_d, d_d].

Fifth time: at this time, because the fifth element has changed to d_d, the value of d_d is assigned to 5th elements again. The result is [a_a, B _ B, c_c, d_d, d_d].

Solution:

1. try not to use the same loop variable name;

2. unset ($ a) before each use or reuse; process and remove the address application

Use the code example above:

$ Arr = ['A', 'B', 'C', 'D', 'E']; foreach ($ arr as $ I =>& $) {$ a = $. '_'. $ a; echo $.'
';} Echo ''; echo $ a; echo''; // unset ($ a); echo $ a = 'CCC'; echo ''; print_r ($ arr); echo ''; foreach ($ arr as $ I => $ a) {echo $.'
';} Echo ''; print_r ($ arr );

Output result:

Now it's normal. pay attention to these small details.

The above is a small Editor for you to talk about all the content in PHP about the use of variables to reference foreach. I hope you can support PHP ~

For more information about the pitfall related to the use of variables by foreach in PHP, please refer to PHP!

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.