php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24486 strange behaviour with references
Submitted: 2003-07-03 11:19 UTC Modified: 2003-07-07 02:45 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: bs@php.net Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 4.3.2 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bs@php.net
New email:
PHP Version: OS:

 

 [2003-07-03 11:19 UTC] bs@php.net
Description:
------------
we just detected a strange behaviour with references and foreach:
after the foreach() is done in the sample code the & disappear in the var_dump for the first two elements, the third is of course still a valid reference because $row exists
i tried to reproduce that effect in different ways but it only happens when using a foreach

Reproduce code:
---------------
<pre><?php
$arr = array(1, 2, 3);
for ($i = 0; $i < count($arr); $i++) {
    $row =& $arr[$i];
    $row = $row * $row;
}
var_dump($arr);

reset ($arr);
while (list(, $value) = each ($arr))
    echo "value: $value\n";

var_dump($arr);

foreach($arr AS $value)
    echo "value: $value\n";

var_dump($arr);
?>

Actual result:
--------------
array(3) {
  [0]=>
  &int(1)
  [1]=>
  &int(4)
  [2]=>
  &int(9)
}
value: 1
value: 4
value: 9
array(3) {
  [0]=>
  &int(1)
  [1]=>
  &int(4)
  [2]=>
  &int(9)
}
value: 1
value: 4
value: 9
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(4)
  [2]=>
  &int(9)
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-03 14:45 UTC] sniper@php.net
And you know that foreach() operates with a copy of the original array..? And this reference thing is problem..why?

 [2003-07-07 02:45 UTC] sniper@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 05:01:28 2024 UTC