note 56090 deleted from language.variables.variable by mazzanet
| From: | mazzanet@php.net | Date: | Tue, 23 Aug 2005 07:47:36 +0000 |
| Subject: | note 56090 deleted from language.variables.variable by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-94069@lists.php.net to get a copy of this message | ||
Note Submitter: ia [AT] zoznam [DOT] sk
Reason: useless
----
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'.