php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22518 Accessing superglobal arrays in functions via variable variable names
Submitted: 2003-03-03 08:06 UTC Modified: 2003-03-04 04:57 UTC
Votes:2
Avg. Score:1.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: rabus at users dot sourceforge dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2003-03-03 (dev) OS: Windows NT
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rabus at users dot sourceforge dot net
New email:
PHP Version: OS:

 

 [2003-03-03 08:06 UTC] rabus at users dot sourceforge dot net
Please try the following script:

<?php
function foo() {
    echo $_SERVER;
}
foo();
?>

This script returns "Array". This is the expected behavior.
Now, try this one:

<?php
function foo() {
    $bar = '_SERVER';
    echo $$bar;
}
foo();
?>

This time, an error is returned:
Notice: Undefined variable: _SERVER on line 4
This is nonsense, of course: As we see in the example above, the variable should be defined!

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-03 09:22 UTC] daniel dot gorski at develnet dot org
Similar malfunction here:

<?php
  error_reporting(E_ALL);
  print_r($_SERVER);
  print_r($_ENV);
?>

PHP Notice:  Undefined variable:  _SERVER in /var/webservers/cowiki/htdocs/test.php on line 6
PHP Notice:  Undefined variable:  _ENV in /var/webservers/cowiki/htdocs/test.php on line 7

Todays CVS, build as CGI on RH6.2

regards dtg
 [2003-03-03 20:14 UTC] sniper@php.net
RTFM:

http://www.php.net/manual/en/language.variables.predefined.php#language.variables.predefined

"Variable variables:  Superglobals cannot be used as variable variables."

 [2003-03-04 04:57 UTC] rabus at users dot sourceforge dot net
I am sorry for this bogus report.
Since German is my mother tounge, I read the German edition of your manual. Unfortunately, the note you mentioned is missing there.
From now on, I'll read the English one, only, since the German one appears a little out-of-date to me.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Oct 27 21:00:02 2025 UTC