PHP.nl

$GLOBALS

$GLOBALS

References all variables available in global scope

An associative containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array. array

Voorbeeld: example

<?php

function test()
{
    $foo = "local variable";

    echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
    echo '$foo in current scope: ' . $foo . "\n";
}

$foo = "Example content";
test();

?>
$foo in global scope: Example content
$foo in current scope: local variable

Waarschuwing: > As of PHP 8.1.0, write access to the entire array is no longer supported:

$GLOBALSVoorbeeld: writing entire will result in error.

<?php

// Generates compile-time error:
$GLOBALS = [];
$GLOBALS += [];
$GLOBALS =& $x;
$x =& $GLOBALS;
unset($GLOBALS);
array_pop($GLOBALS);
// ...and any other write/read-write operation on $GLOBALS

?>

Opmerking: > ### Variable availability

Unlike all of the other ,
 has essentially always been available in PHP.

superglobals$GLOBALS

Opmerking: > As of PHP 8.1.0, is now a read-only copy of the global . That is, global variables cannot be modified via its copy. Previously, array is excluded from the usual by-value behavior of PHP arrays and global variables can be modified via its copy.

$GLOBALSsymbol table`$GLOBALS````php

$value) { $GLOBALS[$key] = $value; } ?>

Documentatie