From: declan at context dot ie Operating system: linux PHP version: Irrelevant PHP Bug Type: Arrays related Bug description: global $$formvar fails if $formvar == "array[element]"
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] = -- Edit bug report at http://bugs.php.net/?id=25692&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=25692&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=25692&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=25692&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=25692&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=25692&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=25692&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=25692&r=support Expected behavior: http://bugs.php.net/fix.php?id=25692&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=25692&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=25692&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=25692&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=25692&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=25692&r=dst IIS Stability: http://bugs.php.net/fix.php?id=25692&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=25692&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=25692&r=float