|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-03-01 09:51 UTC] goba@php.net
See the attached example. The first print_r() in the
function does not print out anything, while the second
prints out the contents of $_GET. I have set $_GET to a
dummy array to let you test without a server.
Conclusion: dynamic names does not work for superglobals
in functions (I have also tested them in methods, but
these handled the same as functions...). Though
dynamic names work in global scope for superglobals...
<?php
$_GET = array("aa");
$method = "_GET";
$var = $$method;
echo "\n$method vars in global scope:\n";
print_r($var); // prints out the array
function test ()
{
$method = "_GET";
$var = $$method;
echo "\n$method vars in test func:\n";
print_r($var); // prints out nothing
echo "\n_GET vars in test func:\n";
$var = $_GET;
print_r($var); // prints out the array
}
test();
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 09:00:01 2025 UTC |
The strange thing is that it works in global scope, but not in function scope. I quess people will try to use this for similar thing such as $var = "HTTP_" . $method . "_VARS"; global $$var; $form = $$var; or $form = $GLOBALS["HTTP_" . $method . "_VARS"]; But using $form = ${"_$method"}; is much more simple, and requires no globals... This is inconsistent as it works outside of functions but not inside of functions, although we advertise the superglobals, as they behave the same inside or outside any scope... I hit this 'bug' because I wanted to use exactly this short thing...