Hi!
Without much ado, here is a piece of problematic code:
<?php
ini_set('error_report', E_ALL);
function f1() {
global $a;
$array = array();
$throwaway = f3($array);
var_dump($a);
}
function f2() {
global $a;
$array = array();
f3($array);
var_dump($a);
}
function f3($array) {
global $a;
$array[0] = &$a;
$array[0] = &$array[0]['foo']; // no notice here. interesting.
$array[0] = 'bar';
return $array;
}
$a = array();
f1();
$a = array();
f2();
?>
Please note that the only difference between f1 and f2 that the first
records the return value of f3() into a variable. However, this changes
the output:
array(1) {
["foo"]=>
&string(3) "bar"
}
array(1) {
["foo"]=>
string(3) "bar"
}
Is this a bug? Or I miss something?
Thanks
Karoly Negyesi
Ps. There is a good reason for using such ugly reference magic, and I am
happy to explain it to anyone who is interested, but it's a long story...
Hi,
Karoly Negyesi a écrit :
Please note that the only difference between f1 and f2 that the first
records the return value of f3() into a variable. However, this
changes the output:array(1) {
["foo"]=>
&string(3) "bar"
}
array(1) {
["foo"]=>
string(3) "bar"
}Is this a bug? Or I miss something?
When copying an array containing a reference, the reference is keeped. :
$value = 'foo';
$array = array (&$value);
$array2 = $array;
$array2[0] = 'bar';
echo $array[0]; // bar
Here, the &string(3) means that it's referenced. Indeed, $throwaway
contains a reference to it.
change your f1 to this :
function f1() {
global $a;
$array = array();
$throwaway = f3($array);
unset($throwaway);
var_dump($a);
}
And the "&" will obviously disappear, as the reference no longer exists.
Kind regards
-- Etienne Kneuss http://www.colder.ch/ colder@php.net
--
Etienne Kneuss