Bug #67916 [NEW]: Reference does not work as expected in array
| From: | qerazor at googlemail dot com | Date: | Wed, 27 Aug 2014 14:44:55 +0000 |
| Subject: | Bug #67916 [NEW]: Reference does not work as expected in array | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-187296@lists.php.net to get a copy of this message | ||
From: qerazor at googlemail dot com
Operating system: Ubuntu
PHP version: Irrelevant
Package: Arrays related
Bug Type: Bug
Bug description:Reference does not work as expected in array
Description:
------------
PHP Version 5.5.9-1ubuntu4.1 but am not sure if that maters in this
case.
one of the foreach takes the value as ref. the other foreach does not.
as you can see in the result of the script, the dump of this array after
manipulating after the first foreach is correct. but the comparison
within the second foreach is wrong.
to get that resolved i have to rename one of the $value variables or use
the $value variable in the second foreach also as ref.
Test script:
---------------
$array = array(
'a'=> 'foo',
'b'=> 'foo',
'c'=> 'foo'
);
var_dump($array);
foreach($array as &$value) {
$value = false;
}
var_dump($array);
$array['b'] = true;
var_dump($array);
foreach($array as $key => $value) {
echo $key.' = '.((int)$value).'<br>';
}
Expected result:
----------------
array (size=3)
'a' => string 'foo' (length=3)
'b' => string 'foo' (length=3)
'c' => string 'foo' (length=3)
array (size=3)
'a' => boolean false
'b' => boolean false
'c' => boolean false
array (size=3)
'a' => boolean false
'b' => boolean true
'c' => boolean false
a = 0
b = 1
c = 0
Actual result:
--------------
array (size=3)
'a' => string 'foo' (length=3)
'b' => string 'foo' (length=3)
'c' => string 'foo' (length=3)
array (size=3)
'a' => boolean false
'b' => boolean false
'c' => &boolean false
array (size=3)
'a' => boolean false
'b' => boolean true
'c' => &boolean false
a = 0
b = 1
c = 1
--
Edit bug report at https://bugs.php.net/bug.php?id=67916&edit=1
--