doc-en icon indicating copy to clipboard operation
doc-en copied to clipboard

Undefined variable variable $GLOBALS

Open jorgsowa opened this issue 2 years ago • 1 comments

Description

The following code:

<?php

$variable = 'GLOBALS';

$GLOBALS['GLOBALS'] = 'value';

var_dump($GLOBALS);

https://3v4l.org/lpfq6

Resulted in this output:

array(8) {
  ["_GET"]=>
  array(0) {
  }
  ["_POST"]=>
...
}

But I expected this output instead:

string(5) "value"

The global variable GLOBALS shouldn't be allowed because it's hidden behind the superglobal.

Another problem with this superglobal: Calling $variable = 'GLOBALS';$$variable; outputs Warning: Undefined variable $GLOBALS, but that's not what the user expects, because $GLOBALS is available. I know it's stated in documentation that superglobal array is not allowed, but this error message could be less confusing. https://www.php.net/manual/en/language.variables.variable.php

PHP Version

PHP 8.4

Operating System

No response

jorgsowa avatar Jan 24 '24 23:01 jorgsowa

As of PHP 8.1, $GLOBALS is not a real variable (so cannot be referenced through variable variables) and it cannot be used to modify variables (as it provides read-only access to the symbol table).

The latter is already documented. The former doesn't appear to be.

damianwadley avatar Jan 24 '24 23:01 damianwadley