phpvariablesvariable-variables

Why variable variables isn't working for $_SESSION?


I am trying to understand the double variables of php. Let's say I have this

$req = '_SESSION';

So, shouldn't $$req be equivalent to $_SESSION? But I am getting an error saying undefined variable '_SESSION'. Please explain this to me.


Solution

  • You can't do that because (from Variable VariablesDocs):

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

    Highlight is by me, $_SESSION is such a Superglobal array.