Error using $ _SERVER in a variable variable (PHP)

I tried to get the name of a superglobal variable through the GET parameter. I was transferred only _VAR_NAME(no $) to get the request, so the program I need to be accessed through the variable variable to him $$_GET['parameter_name'].

Everything went fine except $_SERVER. To try what was wrong, I just made a small PHP script to check what was going on. Here is the code:

<?php
    // ¡¡ This does not work !!
        $nombre = "_SERVER";
        $var = $$nombre;
        print_r($var);
    // This works 
        $nombre = "_GET";
        $var = $$nombre;
        print_r($var);
?>

Is there a reason why the version is _SERVERnot working? I get the following error:

Note: Undefined variable: _SERVERin ...

+3
source share
4 answers

You can try the alternative syntax:

$var = $GLOBALS["_SERVER"];
print_r($var);

$$varvar.

, , - $_SERVER. ( , count($_SERVER); script.)

, variables_order= php.ini ( PHP-.)

+1

, , ( ).

, $nombre, !

switch ($nombre) {
  case "_SERVER" : 
    print_r($_SERVER);
    break;
  case "_GET" : 
    print_r($_GET);
    break;
  case "_POST" : 
    print_r($_POST);
    break;
  // ...
  default:
    echo "Unknown variable";
}
+2

. print_r ($ _SERVER)

.

, - script.


?

, .

switch.

0

auto_globals_jit , SERVER ENV (Just In Time), script. PHP :

SERVER ENV , , .

:

  • PHP getenv() SERVER.
  • $_SERVER; script.
  • ( php.ini: auto_globals_jit = Off script: ini_set('auto_globals_jit',0);)
  • '_SERVER' $GLOBALS ($GLOBALS['_SERVER'])
0

All Articles