Variable Variables and Superglobals

php.internals

A.Rico

22 years ago
Using Variable Variables works fine inside functions for global defined variables if you declare them as global inside the function. Suprinsingly, it does not seem to work with superglobals. Take the following example: // code //////////////////////////////////////////////////////////// <?php $glob_var="Var 1"; $var_glob_var="glob_var"; $var_ENV="_ENV"; echo $_ENV["OS"],"\n"; echo $glob_var,"\n\n"; echo $var_glob_var,"\n"; echo $var_ENV,"\n\n"; echo $$var_glob_var,"\n"; echo ${$var_ENV}["OS"],"\n\n"; foo1($var_glob_var); foo2($var_glob_var); foo3(); foo4(); foo5($var_ENV); foo6($var_ENV); foo7(); foo8(); foo9(); function foo1($arg){ echo "--- In foo1 --------------\n"; echo $arg,"\n"; echo $$arg,"\n"; echo "--------------------------\n"; } function foo2($arg){ global $glob_var; echo "--- In foo2 --------------\n"; echo $arg,"\n"; echo $$arg,"\n"; echo "--------------------------\n"; } function foo3(){ $arg="glob_var"; echo "--- In foo3 --------------\n"; echo $arg,"\n"; echo $$arg,"\n"; echo "--------------------------\n"; } function foo4(){ global $glob_var; $arg="glob_var"; echo "--- In foo4 --------------\n"; echo $arg,"\n"; echo $$arg,"\n"; echo "--------------------------\n"; } function foo5($arg){ echo "--- In foo5 --------------\n"; echo $arg,"\n"; echo ${$arg}["OS"],"\n"; echo "--------------------------\n"; } function foo6($arg){ global $_ENV; echo "--- In foo6 --------------\n"; echo $arg,"\n"; echo ${$arg}["OS"],"\n"; echo "--------------------------\n"; } function foo7(){ $arg="_ENV"; echo "--- In foo7 --------------\n"; echo $arg,"\n"; echo ${$arg}["OS"],"\n"; echo "--------------------------\n"; } function foo8(){ global $_ENV; $arg="_ENV"; echo "--- In foo8 --------------\n"; echo $arg,"\n"; echo ${$arg}["OS"],"\n"; echo "--------------------------\n"; } function foo9(){ echo "--- In foo9 --------------\n"; echo $_ENV["OS"],"\n"; echo "--------------------------\n"; } ?> // Output ///////////////////////////////////////////////////////// Windows_NT Var 1 glob_var _ENV Var 1 Windows_NT --- In foo1 -------------- glob_var -------------------------- --- In foo2 -------------- glob_var Var 1 -------------------------- --- In foo3 -------------- glob_var -------------------------- --- In foo4 -------------- glob_var Var 1 -------------------------- --- In foo5 -------------- _ENV -------------------------- --- In foo6 -------------- _ENV -------------------------- --- In foo7 -------------- _ENV -------------------------- --- In foo8 -------------- _ENV -------------------------- --- In foo9 -------------- Windows_NT -------------------------- // Version ////////////////////////////////////////////// PHP 4.3.7 (cgi-fcgi) (built: Jun 2 2004 15:49:31) Copyright (c) 1997-2004 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies // End ////////////////////////////////////////////////// I'm doing something wrong? Or this is the expected behavior? Thank you in advance

Andi Gutmans

22 years ago
Right. Variable variables don't work with super-globals and I think it is documented in this way. This is on purpose as we decided to only support them this way both for clarity reasons and for performance. There are no plans to change this. Andi At 01:35 AM 8/5/2004 +0200, A.Rico wrote:

A.Rico

22 years ago
What the PHP documentation says about variable variables and superglobals is that you can't use them as a "pointer": /*Warning*/ / Please note that variable variables cannot be used with PHP's Superglobal arrays <http://www.php.net/manual/en/language.variables.predefined.php#language.variables.superglobals>. This means you cannot do things like ${$_GET}. If you are looking for a way to handle availability of superglobals and the old HTTP_*_VARS, you might want to try referencing <http://www.php.net/manual/en/language.references.php> them. / what I intended was to use a "normal" globally defined variable to "point" to a superglobal; that, in fact, works in the global scope, but not in the function scope: // Code ////////////////////////////////////////////////////////////// $varvar="_ENV"; echo $_ENV["OS"]; // --> Windows NT echo ${$varvar}; // --> Windows NT foo($varvar); function foo($arg){ $lvar="_ENV"; echo $_ENV["OS"]; // --> Windows NT echo ${$arg}["OS"]; // --> *nothing* echo ${$lvar}["OS"]; // --> *nothing* var_dump( $$arg ); // --> NULL var_dump( $$lvar); // --> NULL } // END ////////////////////////////////////////////////////////////// Regards, A. Rico

Curt Zirzow

22 years ago
* Thus wrote A.Rico:
> What the PHP documentation says about variable variables and > superglobals is that you can't use them as a "pointer": > > /*Warning*/ > > / Please note that variable variables cannot be used with PHP's > Superglobal arrays > <http://www.php.net/manual/en/language.variables.predefined.php#language.variables.superglobals>. > This means you cannot do things like ${$_GET}. If you are looking > for a way to handle availability of superglobals and the old > HTTP_*_VARS, you might want to try referencing > <http://www.php.net/manual/en/language.references.php> them. / > > what I intended was to use a "normal" globally defined variable to > "point" to a superglobal; that, in fact, works in the global scope, but > not in the function scope: > > // Code ////////////////////////////////////////////////////////////// > $varvar="_ENV"; > > echo $_ENV["OS"]; // --> Windows NT > echo ${$varvar}; // --> Windows NT
This is a documentation issue, i believe, as noted on: http://php.net/variables.predefined Variable variables super globals aren't allowed inside functions or methods. The waring on the page you reference is fixed in the docs reflecting this. Curt
-- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid!

Sara Golemon

22 years ago
> function foo($arg){ > $lvar="_ENV"; > > echo $_ENV["OS"]; // --> Windows NT > echo ${$arg}["OS"]; // --> *nothing* > echo ${$lvar}["OS"]; // --> *nothing* > var_dump( $$arg ); // --> NULL > var_dump( $$lvar); // --> NULL > } > // END ////////////////////////////////////////////////////////////// >
echo $GLOBALS[$lvar]['OS'];