php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #15810 superglobals does not work with dynamic vars in functions
Submitted: 2002-03-01 09:51 UTC Modified: 2010-12-22 14:57 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:2 (50.0%)
From: goba@php.net Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.1.0 OS: win2k
Private report: No CVE-ID: None
 [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();

?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-03-01 09:55 UTC] derick@php.net
It was not supposed to work, so I'm making this a feature request.
If you want  I can dig up some old mail / chat logs about this...

Derick
 [2002-03-01 10:39 UTC] goba@php.net
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...
 [2002-03-01 12:07 UTC] postings dot php dot net at hans-spath dot de
DUPLICATE

I've reported this bug before. Bug #15730.
 [2002-03-01 12:13 UTC] sander@php.net
This is actually a dupe of 15730 but this one has some more info so I'm closing the 15730...
 [2010-12-22 14:57 UTC] johannes@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-12-22 14:57 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You have to reference it once using the proper name or disable JIT in php.ini
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC