#24486 [Fbk->NoF]: strange behaviour with references

From: Date: Mon, 07 Jul 2003 07:45:02 +0000
Subject: #24486 [Fbk->NoF]: strange behaviour with references
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-43343@lists.php.net to get a copy of this message
 ID:               24486
 Updated by:       sniper@php.net
 Reported By:      bs@php.net
-Status:           Feedback
+Status:           No Feedback
 Bug Type:         Scripting Engine problem
 Operating System: Linux
 PHP Version:      4.3.2
 New Comment:

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.




Previous Comments:
------------------------------------------------------------------------

[2003-07-03 14:45:01] 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-03 11:19:53] 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)
}


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=24486&edit=1



Thread (3 messages)

« previous php.bugs (#43343) next »