|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80541 foreach by-val vs. by-ref traversal unclear
Submitted: 2020-12-22 09:54 UTC Modified: 2021-07-20 09:48 UTC
Avg. Score:3.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: andre at webkr dot de Assigned:
Status: Verified Package: Arrays related
PHP Version: 8.0.0 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: andre at webkr dot de
New email:
PHP Version: OS:


 [2020-12-22 09:54 UTC] andre at webkr dot de
I'm not sure whether this is a bug or a documentation bug. I'd consider it a bug but given the promise of backwards compatibility it's unlikely to be fixed.

If the iterator variable in a foreach is a reference, then the whole array becomes a reference and it is no longer safe to append to the array. This is true even when the iterator variable is not actually used anywhere.

At least this behavior should be documented. The documentation currently says:

> In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

It says nothing about the array itself, only about the array values, reenforcing the wrong impression that it is still safe to add or remove array elements.

Test script:
$arr = ['a','b','c'];

foreach ($arr as &$dummy)
    $arr []= 'foo';


Expected result:
array(6) {
  string(1) "a"
  string(1) "b"
  string(1) "c"
  string(3) "foo"
  string(3) "foo"
  string(3) "foo"

Actual result:
Fatal error: Allowed memory size of [...] bytes exhausted


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-24 00:20 UTC] a3ron223 at gmail dot com
The foreach function will continue to run until it has reached the end of the array.
If the foreach function adds another key to the array every time it runs, then it will not stop running (until it exhausts the script's memory).

I'm a little surprised that it isn't documented.
 [2021-07-20 09:48 UTC]
-Summary: Using a reference as iterator variable turns the whole array into a reference +Summary: foreach by-val vs. by-ref traversal unclear -Status: Open +Status: Verified
 [2021-07-20 09:48 UTC]
This not related to the fact that $arr is turned into a
reference[1], but rather that foreach by-val traversal actually
happens on a (separated) copy of the original array[2], while
by-ref traversal happens on the original array.

This is by design, so, yes, documentation issue.

[1] <>
[2] <>
 [2022-11-26 13:26 UTC] plumbingadvice24 at gmail dot com
Thank you for this.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 24 17:01:30 2024 UTC