|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2003-09-29 21:30 UTC] sniper@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 14:00:01 2025 UTC |
Description: ------------ If you pass a variable name to a function and then try to dereference that variable name for use in a global statement, then the global statement fails. I believe the problem lies in the way that global is implemented. If I have the following: $ary[foo]="bar"; function use_global($varname) { global $$varname; echo "$varname is set to ". $$varname; } use_global ("ary[foo]"); then I think that the global line is being re-written to be equivalent to something like: $_use_global_varname=&$GLOBALS["ary[foo]"]; whereas, AFAICS, it should be: $_use_global_varname=&$GLOBALS["ary"]["foo"]; I want to be able to use this construct without having to know whether the incoming variable is a plain variable or an array element. Reproduce code: --------------- <?php // -*-php-*- function use_global ($formvar) { $ref=&$GLOBALS[$formvar]; echo "formvar: $formvar = " . $$ref; }; $global=array (); $global["foo"]=1; $global["bar"]=2; $global["baz"]=3; echo "global[bar]=$global[bar]\n"; use_global("global[bar]"); ?> Expected result: ---------------- If things worked correctly, the output should be: global[bar]=2 formvar: global[bar] =2 Actual result: -------------- global[bar]=2 formvar: global[bar] =