Doc #80541 [NEW]: Using a reference as iterator variable turns the whole array into a reference

From: Date: Tue, 22 Dec 2020 09:54:52 +0000
Subject: Doc #80541 [NEW]: Using a reference as iterator variable turns the whole array into a reference
Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-18303@lists.php.net to get a copy of this message
From:             andre at webkr dot de
Operating system: 
PHP version:      8.0.0
Package:          Arrays related
Bug Type:         Documentation Problem
Bug description:Using a reference as iterator variable turns the whole array into a reference

Description:
------------
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';
}

var_dump($arr);

Expected result:
----------------
array(6) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(3) "foo"
  [4]=>
  string(3) "foo"
  [5]=>
  string(3) "foo"
}

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

-- 
Edit bug report at https://bugs.php.net/bug.php?id=80541&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=80541&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=80541&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=80541&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=80541&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=80541&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=80541&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=80541&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=80541&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=80541&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=80541&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80541&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=80541&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=80541&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=80541&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=80541&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=80541&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=80541&r=mysqlcfg


Thread (6 messages)

« previous php.doc.bugs (#18303) next »