What is the difference between global and superglobal variables?

I read some time ago that PHP has the vast majority of its variables declared with local scope.

But I found two other concepts variables global and super global and I didn't quite understand the difference between them.

  • What is the difference between global and super global variables?
  • when should one (ria) use each specific type?
Author: UzumakiArtanis, 2017-08-07

2 answers

Super Global Variables

Super global variables are native PHP variables and are named because they will be present in any scope of the program. They were introduced in PHP 4.1.0 and are:

  • $GLOBALS : reference all variables available in the global scope;
  • $_SERVER : server information and runtime environment;
  • $_GET : HTTP request variables GET;
  • $_POST : HTTP POST request variables;
  • $_FILES : files sent via HTTP POST;
  • $_COOKIE : Cookies set in HTTP request;
  • $_SESSION : session variables;
  • $_REQUEST : HTTP request variables;
  • $_ENV : environment variables;

The behavior of super global variables can be affected with settings like variables_order e register_globals. Being the super global variables native, the developer is not allowed to define them. That is, there will be no other super global but the aforementioned, unless in future versions of PHP another is defined.

Variable variables

One behavior of super global variables that differs from other variables is that they do not suffer from the behavior of variable variables in local scopes, either within functions or methods. That is, considering the code below:

function get_id() {

    $var = "_GET";

    return ${$var}["id"];
}

See working on Ideone .

Error will be thrown:

Undefined variable: _GET

Even if you happen to want to include the variable in the scope (which doesn't make sense):

function get_id() {

    global $_GET;

    $var = "_GET";

    return ${$var}["id"];
}

See working on Ideone .

Global Variables

In turn, global variables are common variables defined in the scope Application global, however, unlike some other programming languages, global variables are not defined in all scopes by default, and it is necessary to inform when a variable is outside the local scope. This can be done using global. For example, consider the code below:

$x = 1;

echo $x, PHP_EOL;

function foo() {
    $x = 2;

    echo $x, PHP_EOL;
}

foo();

echo $x, PHP_EOL;

See working on Ideone .

Considering the natural behavior of global variables, the expected output would be 1, 2, 2, since the value of $x would be modified within the function, but in PHP the output is 1, 2, 1, because what happens is that the variable $x is local, not affecting the value of the global variable. However, if you indicate $x as global:

$x = 1;

echo $x, PHP_EOL;

function foo() {
    global $x;

    $x = 2;

    echo $x, PHP_EOL;
}

foo();

echo $x, PHP_EOL;

See working on Ideone

The output, yes, will be 1, 2, 2, since now the global variable has been affected by the function.

Anonymous functions

The same happens with anonymous functions, when necessary to use external variables. For example, to multiply a list of values by a factor:

$array = [1, 2, 3, 4, 5];
$factor = 2;

function multiplica($array) {

    $factor = 5;

    $array = array_map(function ($value) {
        global $factor;
        return $value * $factor;
    }, $array);

    return $array;

}

print_r(multiplica($array));

See working on Ideone .

The result will be the multiplication of the list by factor 2, because when using global $factor the variable $factor is imported from the global scope, not from the larger scope than the current one. If the global variable does not exist, an unexpected result or error is produced.

Variables variables

And contrary to the behavior of super global, global variables work with variable variables.

$foo = "SOpt";

function foo () {

    global $foo;

    $var = "foo";

    echo $$var, PHP_EOL;
}

foo();

See working on Ideone .

Provided that obviously the global variable is included in the scope of the function using the global directive. Otherwise, the variable is not set, generating the error:

$foo = "SOpt";

function foo () {
    $var = "foo";

    echo $$var, PHP_EOL;
}

foo();

See working on Ideone .

All global and super global variables will be in the array associative defined in $GLOBALS. Being this a super global variable, it can be used in any scope, but it is worth remembering that local variables to this scope will not be included in the array, only the global ones.

When should one (ria) use each specific type?

The Super global variables you will use when you need some value defined by PHP in the variables quoted at the beginning of the answer. That is, if you need to access the value $_SESSION, or retrieve information from the web server, $_SERVER. COO said earlier, there is no way the developer set a new super global variable, just use them when needed. In turn, global variables should be used when this makes sense for their application. That is, when a function makes use of some external value and it is not semantic to pass it by parameter. A practical example could be an application log file; assuming that there exists the function add, which adds two values and generates the respective log:

function add($x, $y, $handle_log) {
    $result = $x + $y;

    fwrite($handle_log, "O resultado da soma {$x}+{$y} foi {$result}");

    return $result;
}

The use of the function would be something like this:

$handle_log = fopen("log.txt", "w");

$result = add(1, 2, $handle_log);

echo $result;

Realize that it doesn't make much sense to have to pass $handle_log as a parameter of a function and its goal is to just add two values. An alternative would be:

$handle_log = fopen("log.txt", "w");

function add($x, $y, ) {
    global $handle_log;

    $result = $x + $y;

    fwrite($handle_log, "O resultado da soma {$x}+{$y} foi {$result}");

    return $result;
}

$result = add(1, 2);

echo $result;

Making the function call make sense according to the purpose of the function. There are many other alternatives besides this, possibly much more elegant, but it is an example. In short, you can / should use a global variable when it makes sense for your solution.


Interesting readings:

Why is using global variables not good practice?

 14
Author: Woss, 2017-08-07 21:07:00

The difference is that the Super globals there is no need to inform global $variavel, you simply access. They are available in all scopes, they are:

  1. glob GLOBALS
  2. $_SERVER
  3. $_GET
  4. $_POST
  5. $_FILES
  6. $_COOKIE
  7. $_SESSION
  8. $_REQUEST
  9. $_ENV

Already the global variables (not the super ones), to have access to it in different scopes, you need to inform global $variavel before using it (in each scope). Only after this command will it be available to you in the code.

 10
Author: Bruno Rigolon, 2017-08-07 20:07:26