Home >Backend Development >PHP Tutorial >PHP leaves array reference problem after foreach loop
The content of this article is about the reference problem of the array left by PHP after the foreach loop. Now I share it with you. Friends in need can take a look.
eg:
result:
Explanation:
1.foreach It is not a block-level field, the array reference ends, $k, $a does not release memory
2. When the first reference cycle ends, $a points to the last element of the array4
3 .When the loop starts for the second time (at this time $a already points to the last element and is a reference)
Loop 4 times. The changes in the array are:
1-> [1,2,3,4]
2-> [1,2,3,4]
3-> [1, 2,3,3] The third time value points to the last element 3 But value is a global reference pointing to the last element in the last cycle, so when value=3 it will also be The value of the last element is changed to 3
4-> [1,2,3,3]
foreach reference passing address in PHP
Exception handling after using & reference in foreach in php
Analysis between foreach and normal for loop efficiency
The above is the detailed content of PHP leaves array reference problem after foreach loop. For more information, please follow other related articles on the PHP Chinese website!