Bug #68586 [NEW]: Variable losing memory address when used by function that passes by reference
| From: | hmdiana at gmail dot com | Date: | Wed, 10 Dec 2014 18:21:25 +0000 |
| Subject: | Bug #68586 [NEW]: Variable losing memory address when used by function that passes by reference | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-188996@lists.php.net to get a copy of this message | ||
From: hmdiana at gmail dot com
Operating system: All
PHP version: 5.4.35
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Variable losing memory address when used by function that passes by reference
Description:
------------
When inside a foreach and using a function that should get the variable
passed by reference, that function changes the memory address of that
variable so,instead of working with the original variable, it works with
a copy of it and the original variable never get the changes we made.
Test script:
---------------
$things = array(
909 => array(
'name' => 'Colors',
'children' => array(
0 => array('name' => 'Yellow', 'id' => 855),
1 => array('name' => 'Blue', 'id' => 842),
2 => array('name' => 'Brown', 'id' => 843),
3 => array('name' => 'Red', 'id' => 856)
)
)
);
foreach ($things as $thing) {
if (isset($thing['children'])) {
array_multisort($thing['children']);
}
}
Expected result:
----------------
The array 'children' on alphabetical order, ordered by 'name':
array (size=10)
909 =>
array (size=4)
'name' => string 'Colors' (length=9)
'children' =>
array (size=14)
0 =>
array (size=2)
'name' => string 'Blue' (length=9)
'id' => string '842' (length=3)
1 =>
array (size=2)
'name' => string 'Brown' (length=8)
'id' => string '843' (length=3)
2 =>
array (size=2)
'name' => string 'Red' (length=5)
'id' => string '856' (length=3)
3 =>
array (size=2)
'name' => string 'Yellow' (length=5)
'id' => string '855' (length=3)
Actual result:
--------------
The initial variable, as if nothing has changed:
array (size=10)
909 =>
array (size=4)
'name' => string 'Colors' (length=9)
'children' =>
array (size=14)
0 =>
array (size=2)
'name' => string 'Yellow' (length=9)
'id' => string '855' (length=3)
1 =>
array (size=2)
'name' => string 'Blue' (length=8)
'id' => string '842' (length=3)
2 =>
array (size=2)
'name' => string 'Brown' (length=5)
'id' => string '843' (length=3)
3 =>
array (size=2)
'name' => string 'Red' (length=5)
'id' => string '856' (length=3)
--
Edit bug report at https://bugs.php.net/bug.php?id=68586&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68586&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68586&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68586&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68586&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68586&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68586&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68586&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68586&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68586&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68586&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68586&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68586&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68586&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68586&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68586&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68586&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68586&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68586&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68586&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68586&r=mysqlcfg