Skip to content

iterating over the same array twice, first by reference and then by value seems to alter the original array #21283

@aliber4079

Description

@aliber4079

Description

The following code:

<?php
$criteria=[
        "a",
        "b",
];
var_dump($criteria);
foreach ($criteria as &$criterion) {
}
foreach ($criteria as /*&*/$criterion){
}                              
var_dump($criteria);

Resulted in this output:

array(2) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
}
array(2) {
  [0]=>
  string(1) "a"
  [1]=>
  &string(1) "a"
}

But I expected this output instead:

array(2) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
}
array(2) {
  [0]=>
  string(1) "a"
  [1]=>
  &string(1) "b"
}

PHP Version

PHP 8.5.3 (cli) (built: Feb 10 2026 18:44:09) (ZTS Visual C++ 2022 x64)
Copyright (c) The PHP Group
Built by The PHP Group
Zend Engine v4.5.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.3, Copyright (c), by Zend Technologies

Operating System

windows x64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions