# foreach with referenced values

- Thread: https://externals.io/message/26906
- Original archive: https://news-web.php.net/php.internals/26906
- Started by Matthias Pigulla on 2006-12-12T09:42:38+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #26906 — Matthias Pigulla — 2006-12-12T09:42:38+00:00

URL: https://externals.io/message/26906#26906

Hi internals,

please consider:

<?php
 print `PHP_VERSION` . "\n";
 $a = array('a', 'b');
 foreach ($a as &$ref) ;
 $b = $a;
 $a[0] = '*';
 print_r($b);
?>

Result:
5.2.0
Array
(
    [0] => *
    [1] => b
)

Same with 5.1.4 and 5.1.6. I suppose that using foreach and referencing
the values turns the elements in $a into references, $b is a copy of the
array containing *references* and thus chaning the value of the first
element in $a is visible in $b because both arrays reference the the
same value?

When moving the $b assignment before the foreach, everything works as
expected.

Is this really intended behaviour?

Kind regards,
mp.

---

## #26907 — Antony Dovgal — 2006-12-12T09:54:15+00:00

URL: https://externals.io/message/26906#26907 · In reply to: #26906

> Hi internals,
> 
> please consider:
> 
> <?php
>  print `PHP_VERSION` . "\n";
>  $a = array('a', 'b');
>  foreach ($a as &$ref) ;
>  $b = $a;
>  $a[0] = '*';
>  print_r($b);
> ?>
> 
> Result:
> 5.2.0
> Array
> (
>     [0] => *
>     [1] => b
> )

Did you try a snapshot?

5.2.1-dev
Array
(
    [0] => a
    [1] => b
)



-- 
Wbr, 
Antony Dovgal
