W3docs

Understanding PHP Superglobals and the $_GLOBALS Variable

PHP is a popular server-side scripting language used for web development. It provides a number of superglobals, which are built-in variables that are always

In PHP, a variable declared outside any function lives in the global scope. By default, code inside a function cannot see those variables — PHP keeps function scope separate so local logic doesn't accidentally clobber unrelated data. The $GLOBALS array is one way to reach across that boundary: it is a built-in superglobal that holds a reference to every variable defined in the global scope, keyed by variable name.

This page explains what $GLOBALS is, how it differs from the global keyword, how to read and modify global variables through it, and why you should usually reach for an alternative instead. If you are new to scope in general, start with Variable Scope and PHP Variables.

What are PHP Superglobals?

Superglobals are special, predefined PHP variables that are available in all scopes of a script. You can read them inside any function, method, or included file without first declaring them with global. They are populated automatically by PHP before your code runs.

The full list of superglobals:

  • $_GET — data passed via the URL query string.
  • $_POST — data passed via a form submission.
  • $_COOKIE — data passed via cookies.
  • $_SESSION — data stored in the user's session.
  • $_REQUEST — combined $_GET, $_POST, and $_COOKIE data.
  • $_SERVER — information about the server and execution environment.
  • $_ENV — environment variables.
  • $_FILES — items uploaded via an HTTP POST form.
  • $GLOBALS — a reference to every variable in the global scope.

For a tour of the request-related superglobals, see PHP Superglobals.

What is the $GLOBALS Variable in PHP?

$GLOBALS is an associative array whose keys are the names of your global variables (without the leading $) and whose values are references to those variables. Because it is a superglobal, it works from anywhere — so $GLOBALS['x'] reads the same storage as the top-level $x, no matter where in the script you access it.

Here's an example of how the $GLOBALS variable can be used to access a global variable:

PHP example of how the $GLOBALS variable can be used to access a global variable

<?php

$x = 10;
$y = 20;

function add() {
  global $x, $y;
  return $x + $y;
}

echo add(); // 30

echo $GLOBALS['x']; // 10
echo $GLOBALS['y']; // 20

?>

In this example, the add() function uses the global keyword to access the $x and $y variables. Outside the function, $GLOBALS['x'] and $GLOBALS['y'] reach the very same variables — no global declaration required.

$GLOBALS vs. the global keyword

These two approaches do the same job but in different ways:

  • The global keyword imports a global variable into the local scope, creating a local alias you then use by its normal name ($x). You must list every variable you want.
  • $GLOBALS is an array you index by name ($GLOBALS['x']). You do not declare anything, and you can build the key dynamically (for example $GLOBALS[$name]).
<?php

$total = 100;

function withGlobalKeyword() {
  global $total;
  return $total;        // uses the local alias
}

function withGlobalsArray() {
  return $GLOBALS['total']; // indexes the superglobal directly
}

echo withGlobalKeyword(); // 100
echo "\n";
echo withGlobalsArray();  // 100
?>

Both print 100. Prefer whichever reads more clearly — but note that, as of PHP 8.1, you can no longer reassign the $GLOBALS array as a whole (e.g. $GLOBALS = [...]). Writing to individual elements such as $GLOBALS['x'] = 5 still works.

How to Use the $GLOBALS Variable in PHP

The $GLOBALS variable is a powerful tool in PHP programming that can be used to access global variables from any part of your script. Here are a few ways to use the $GLOBALS variable:

Accessing Global Variables

As we saw in the previous example, you can use the $GLOBALS variable to access any global variable defined in your script. Here's another example:

PHP access any global variable defined in your script

php— editable, runs on the server

Modifying Global Variables

The $GLOBALS variable can also be used to modify global variables. This can be useful if you need to make changes to a global variable from within a function or class.

Here's an example:

PHP modify global variables

<?php

$counter = 0;

function increment() {
  global $counter;
  $counter++;
}

increment();
echo $counter; // 1

echo "\n";

$GLOBALS['counter'] = 10;
echo $counter; // 10

?>

In this example, the increment() function increments the value of the $counter variable. However, you can also modify the value of the $counter variable directly through the $GLOBALS variable.

Pass Variables between Functions and Classes

The $GLOBALS variable can also be used to pass variables between functions and classes. This can be useful if you need to share data between different parts of your script.

Here's an example:

PHP passing global variables between functions and classes

<?php

$data = array("name" => "John Doe", "age" => 30);

function display_data() {
  global $data;
  print_r($data);
}

class User {
  function show_data() {
    global $data;
    print_r($data);
  }
}

display_data(); // Array ( [name] => John Doe [age] => 30 )

$user = new User();
$user->show_data(); // Array ( [name] => John Doe [age] => 30 )

$GLOBALS['data']['email'] = "[email protected]";

display_data(); // Array ( [name] => John Doe [age] => 30 [email] => [email protected] )
$user->show_data(); // Array ( [name] => John Doe [age] => 30 [email] => [email protected] )

?>

In this example, the display_data() function and the User class both access the $data variable through the $GLOBALS variable. This allows you to pass the $data variable between the different parts of your script.

Conclusion

The $GLOBALS variable is a powerful tool in PHP programming that provides access to all global variables defined in a PHP script. Whether you're accessing, modifying, or passing variables between functions and classes, the $GLOBALS variable is a versatile tool that can help you write better PHP code.

Note on best practices: While $GLOBALS is useful, relying heavily on global variables is generally discouraged in modern PHP development. Global state makes code harder to test and reason about, because any function can quietly change a value another part of the program depends on. Prefer passing variables as function arguments, returning values, or using class properties for better encapsulation and testability. Reach for $GLOBALS mainly for quick scripts or when interoperating with legacy code.

Practice

Practice
In PHP, what are the types of variables available in a global scope?
In PHP, what are the types of variables available in a global scope?
Was this page helpful?