2

Today I met such a terrible situation. It seems this bug is related to PHP.

I'm trying to access to $_SERVER or another super global variables but from string name.

This version of implementation is working.

var_dump(${"_SERVER"}); // working

But when trying to do this with variable then receiving notice that variable not found.

$var_name = "_SERVER";
var_dump(${$var_name}); // Notice</b>:  Undefined variable: _SERVER in...

And this will happen only with a global variable.

What is going on there? Can someone explain this situation.

Community
  • 1
  • 1
Aaron Yordanyan
  • 738
  • 7
  • 19

1 Answers1

4

variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. Demo

Refer to php doc Variable variables

Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.

LF00
  • 27,015
  • 29
  • 156
  • 295