php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25692 global $$formvar fails if $formvar == "array[element]"
Submitted: 2003-09-29 10:50 UTC Modified: 2003-09-29 21:30 UTC
From: declan at context dot ie Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: linux
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: declan at context dot ie
New email:
PHP Version: OS:

 

 [2003-09-29 10:50 UTC] declan at context dot ie
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] =

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-09-29 21:30 UTC] sniper@php.net
Try setting error_reporting to E_ALL and you'll find out why it doesn't work.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC