PHP 7.0.6 Released

Superglobals

SuperglobalsSuperglobals are built-in variables that are always available in all scopes

Description

Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.

These superglobal variables are:

Changelog

Version Description
4.1.0 Superglobals were introduced to PHP.

Notes

Note: Variable availability

By default, all of the superglobals are available but there are directives that affect this availability. For further information, refer to the documentation for variables_order.

Note: Dealing with register_globals

If the deprecated register_globals directive is set to on then the variables within will also be made available in the global scope of the script. For example, $_POST['foo'] would also exist as $foo.

For related information, see the FAQ titled "How does register_globals affect me?"

Note: Variable variables

Superglobals cannot be used as variable variables inside functions or class methods.

User Contributed Notes

kitchin
2 years ago
Since PHP 5.4, you cannot use a superglobal as the parameter to a function. This causes a fatal error:

function foo($_GET) {
  // whatever
}

It's called "shadowing" a superglobal, and I don't know why people ever did it, but I've seen it out there. The easy fix is just to rename the variable $get in the function, assuming that name is unique.

There was no deprecation warning issued in previous versions of PHP, according to my testing, neither in 5.3 nor 5.2. The error messages in 5.4 are:
Fatal error: Cannot re-assign auto-global variable _GET in...
Fatal error: Cannot re-assign auto-global variable _COOKIE in...
etc.
John Slegers
2 years ago
Want to know how to generate a formatted list with all globals (including custom ones)? Check out the code below.

<?php
// Generate a formatted list with all globals
//----------------------------------------------------
// Custom global variable $_CUSTOM
$_CUSTOM = array('USERNAME' => 'john', 'USERID' => '18068416846');

// List here whichever globals you want to print
// This could be your own custom globals
$globals = array(
   
'$_SERVER' => $_SERVER, '$_ENV' => $_ENV,
   
'$_REQUEST' => $_REQUEST, '$_GET' => $_GET,
   
'$_POST' => $_POST, '$_COOKIE' => $_COOKIE,
   
'$_FILES' => $_FILES, '$_CUSTOM' => $_CUSTOM
);
?>
<html>
    <style>
        <?php // Adjust CSS formatting for your output  ?>
        .left {
            font-weight: 700;
        }
        .right {
            font-weight: 700;
            color: #009;
        }
        .key {
            color: #d00;
            font-style: italic;
        }
    </style>
    <body>
        <?php
       
// Generate the output
       
echo '<h1>Superglobals</h1>';
        foreach (
$globals as $globalkey => $global) {
            echo
'<h3>' . $globalkey . '</h3>';
            foreach (
$global as $key => $value) {
                echo
'<span class="left">' . $globalkey . '[<span class="key">\'' . $key . '\'</span>]</span> = <span class="right">' . $value . '</span><br />';
            }
        }
       
?>
    </body>
</html>
lskatz at gmail dot com
7 years ago
Tibor:
It's not a good idea to use $_ENV unless you are specifying an environmental variable.  This is probably a better example that I found on another page in php.net

<?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();
?>
serpent at paradise dot net dot nz
3 years ago
You can go the other way as well i.e.

<?php
function test() {
       
$GLOBALS['foo'] = "Example content";
}

test();
echo
"<p>$foo</p>";
?>

This doesn't appear to be affected by register_globals, I have it switched off.
To Top