note 56090 added to language.variables.variable
| From: | ia | Date: | Tue, 23 Aug 2005 06:56:48 +0000 |
| Subject: | note 56090 added to language.variables.variable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94064@lists.php.net to get a copy of this message | ||
As for using superglobals with variable variables (PHP 5.0.4):
<?php
// Calling variable with its name given in superglobal - OK!
function dump_variable ($name, $varName) {
$$varName = 'TEST';
echo ${$_GET[$name]};
}
$_GET['hello'] = 'world';
dump_variable ('hello', 'world');
?>
Works no problem, prints 'TEST'.
But this does not work:
<?php
// Echoing content of superglobal variable - fails!
function dump_superglobal ($method, $name) {
echo "'dumping $method['$name']: " . ${$method}[$name];
}
$_GET['hello'] = 'world';
dump_superglobal ('_GET', 'hello');
?>
It ends with the error
Notice: Undefined variable: _GET in /network/web/test.php on line 7
Here's solution:
<?php
// Echoing content of superglobal variable 2nd try - OK!
function dump_superglobal ($method, $name) {
// This does solve the problem!
global $$method;
echo "'dumping $method['$name']: " . ${$method}[$name];
}
$_GET['hello'] = 'world';
dump_superglobal ('_GET', 'hello');
?>
Prints: 'dumping $_GET['hello']: world'.
----
Manual Page -- http://www.php.net/manual/en/language.variables.variable.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56090
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56090&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56090&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56090&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56090&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56090&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56090&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56090&report=yes
Search -- http://master.php.net/manage/user-notes.php