Home >Backend Development >PHP Tutorial >Why does the last element of a PHP array appear duplicated after using foreach with pass-by-reference?

Why does the last element of a PHP array appear duplicated after using foreach with pass-by-reference?

Susan Sarandon
Susan SarandonOriginal
2024-11-15 00:18:021046browse

Why does the last element of a PHP array appear duplicated after using foreach with pass-by-reference?

PHP Foreach Pass by Reference: Mysterious Element Duplication

Introduction:

Have you encountered an odd phenomenon where the last element of your PHP array appears duplicated after iterating over it using foreach with pass-by-reference? This puzzling behavior often raises questions about potential bugs or strange system behavior.

Understanding the Behavior:

The key to understanding this is the nature of pass-by-reference in PHP's foreach loops. When using '&' in the parameter, the reference to the array element is directly modified, rather than a copy. This means that any changes made to the passed variable within the loop directly affect the original array.

The Bug in Question:

In the example provided:

$arr = ["foo", "bar", "baz"];

foreach ($arr as &$item) {}
foreach ($arr as $item) {}

The first foreach loop establishes references to the array elements. Since $item is now a reference, modifying it within the loop will also change the corresponding array element.

The second foreach loop, however, does not establish references. This means that when the loop assigns new values to $item, it only affects the local copy and does not update the original array.

The Duplication Effect:

Now, let's consider the case where $item is a reference to the last array element (i.e., 'baz'). Since the first loop establishes this reference, the second loop also utilizes the same reference. As a result, any value assigned to $item in the second loop gets passed directly to $arr[2].

Therefore, when the second loop assigns 'bar' to $item, it inadvertently replaces the last element of the array with 'bar,' leading to the apparent duplication.

Expected vs. Actual Output:

Before the loops: ["foo", "bar", "baz"]

After the first loop (reference): ["foo", "bar", "foo"] (since $item is still referenced to $arr[2])

After the second loop (non-reference): ["foo", "bar", "bar"]

Is It a Bug?

No, this behavior is not a bug. It is simply the expected consequence of using references in PHP's foreach loops. The value of $item, being a reference to the original array element, affects the underlying array whenever it is modified.

The above is the detailed content of Why does the last element of a PHP array appear duplicated after using foreach with pass-by-reference?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn