|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-01-20 19:27 UTC] tommyhp2 at yahoo dot com
Description:
------------
use of variable variables broken when reference inside a function in conjunction with $_SESSION.
Thanks,
Tommy
Reproduce code:
---------------
Scenario 1:
<code>
session_start();
$arr = array('idx1'=>'val1','idx2'=>'val2','idx3'=>'val3');
if (isset($_SESSION['arr']))
{
$arr =& $_SESSION['arr'];
} else {
$_SESSION =& $arr;
}
</code>
Works as intended (please see "Expected result").
Scenario 2:
<code>
function register_session($var)
{
global $$var;
if (isset($_SESSION[$var]))
{
$$var =& $_SESSION[$var];
} else
{
$_SESSION[$var] =& $$var;
}
}
session_start();
$arr = array('idx1'=>'val1','idx2'=>'val2','idx3'=>'val3');
register_session('arr');
</code>
Does not work as intended (Please see "Actual result"). The variable $arr is not referenced.
Expected result:
----------------
["_SESSION"]=>
&array(3) {
["idx1"]=>
string(4) "val1"
["idx2"]=>
string(4) "val2"
["idx3"]=>
string(4) "val3"
}
["arr"]=>
&array(3) {
["idx1"]=>
string(4) "val1"
["idx2"]=>
string(4) "val2"
["idx3"]=>
string(4) "val3"
}
Actual result:
--------------
["_SESSION"]=>
&array(1) {
["arr"]=>
array(3) {
["idx1"]=>
string(4) "val1"
["idx2"]=>
string(4) "val2"
["idx3"]=>
string(4) "val3"
}
}
["arr"]=>
array(3) {
["idx1"]=>
string(4) "val1"
["idx2"]=>
string(4) "val2"
["idx3"]=>
string(4) "val3"
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Wed Jun 24 20:00:01 2026 UTC |
I don't understand how is this bogus? I did look at the manual to make sure I used the variable variables and reference correctly. Looking at the code give, if I pass string 'arr' to the function, then $$var == ${'arr'}. There is no ambiguity. If I do a var_dump($$var) equivalent to var_dump(${'arr'}) inside the function right after global $$var, I get the defined global $arr array contents. I didn't see any where in the manual at http://www.php.net/manual/en/language.variables.variable.php about not being able to use it or implement it inside a function since the examples didn't implement inside a function. As for references, I don't think it's used improperly. Since I could have multiple references to a single value: $a = 'some string'; $b =& $a; $c =& $a; echo $b; // gives 'some string' echo $c; // gives 'some string' $a = 'nada'; echo $b; // gives 'nada' which those 2 concepts, this code $arr = array('idx1'=>'val1','idx2'=>'val2','idx3'=>'val3'); if (isset($_SESSION['arr'])) { $arr =& $_SESSION['arr']; } else { $_SESSION =& $arr; } works as intended. If put the if/else inside a function and replace with the appropriate variable along with global definition, why does it break with no reference? Or is referencing not permitted inside a function? Which I don't see in the manual. As for the example in the manual of function foo(&$var) { $var++; } I'm not passing the value or the pointer/reference. I'm passing a string and using the string via variable variables and global and referencing it as variable variables. Thanks, TommySorry, the code in scenario 1 should be: if (isset($_SESSION['arr'])) { $arr =& $_SESSION['arr']; } else { $_SESSION['arr'] =& $arr; } Had a long day :D