← Back to PHP Course | Chapter 2: Output & Input | Lesson 7 of 8

PHP $GLOBALS

A variable declared at the top level of a script is normally invisible inside a function -- PHP functions get their own private variable scope by default. $GLOBALS is a special superglobal array that gives you a way around that: it holds every global-scope variable, accessible by name from anywhere, including deep inside a function.

What $GLOBALS Contains

$GLOBALS is automatically populated by PHP with every variable declared in the global (top-level, outside any function) scope -- the key is the variable's name as a string, and the value is that variable's current value, updated live as the global variable changes.

Note: Use print_r($GLOBALS) during debugging to see a full snapshot of every global variable currently defined in the script.

Warning: $GLOBALS only reflects variables declared at the true top level of the script -- variables local to a function are never automatically added to it.

Example: What $GLOBALS Contains

php
<?php
$siteName = "Cookies Cursor";
print_r($GLOBALS['siteName']);
?>

Reading a Global Variable Inside a Function

A function normally cannot see a global variable directly by name -- $siteName inside a function refers to a completely separate, local $siteName that likely does not exist. $GLOBALS["siteName"] sidesteps this by explicitly reaching into the global scope from inside the function.

Note: Reading via $GLOBALS is a quick way to access one or two global values inside a function without restructuring the function's parameters.

Warning: Relying on many different $GLOBALS reads scattered throughout a function makes it much harder to tell, at a glance, what data that function actually depends on.

Example: Reading a Global Variable Inside a Function

php
<?php
$siteName = "Cookies Cursor";
function showSite() {
    echo $GLOBALS['siteName'];
}
showSite();
?>

Writing to a Global Variable via $GLOBALS

Assigning to $GLOBALS["varName"] = $newValue inside a function changes the actual global variable itself, and that change is visible everywhere in the script after the function returns -- not just a local copy scoped to the function call.

Note: Be intentional about which functions are allowed to modify global state through $GLOBALS -- undisciplined use makes a program's behavior much harder to trace.

Warning: A function that silently modifies global state through $GLOBALS can produce confusing bugs, since its effects are not visible in its parameter list or return type.

Example: Writing to a Global Variable via $GLOBALS

php
<?php
$counter = 0;
function increment() {
    $GLOBALS['counter'] = $GLOBALS['counter'] + 1;
}
increment();
echo $counter;
?>

$GLOBALS vs the global Keyword

The global keyword, used as global $varName; inside a function, imports one specific global variable into that function's local scope by reference -- after that line, $varName inside the function behaves as if it were the actual global variable. $GLOBALS instead gives you array-style access without changing local scope at all.

Note: Use global $varName; when a function needs to work with one or two specific global variables repeatedly throughout its body; use $GLOBALS for occasional, one-off access.

Warning: global $varName; only works for variables that already exist in the global scope at the time the function runs -- it does not create a new global variable out of nothing.

Example: $GLOBALS vs the global Keyword

php
<?php
$siteName = "Cookies Cursor";
function withGlobalKeyword() {
    global $siteName;
    echo $siteName . "\n";
}
function withGlobalsArray() {
    echo $GLOBALS['siteName'];
}
withGlobalKeyword();
withGlobalsArray();
?>

When to Avoid Relying on $GLOBALS

Heavy reliance on $GLOBALS (or the global keyword) tightly couples a function to the specific global variables it happens to reference, making that function much harder to test in isolation or reuse in a different context. Passing values in as parameters and getting results back via return is almost always a cleaner design.

Note: Before reaching for $GLOBALS, ask whether the value could simply be passed into the function as a parameter instead -- it almost always can be.

Warning: A function that depends on $GLOBALS cannot be tested by simply calling it with different inputs -- the caller has to carefully set up the right global state first, which is fragile and easy to get wrong.

Example: When to Avoid Relying on $GLOBALS

php
<?php
function addTax($price, $rate) {
    return $price + ($price * $rate);
}
echo addTax(100, 0.08);
// Passing values as parameters is easier to test than reaching into $GLOBALS
?>
Common Mistakes
  1. Overusing $GLOBALS as a way to avoid passing values into functions as proper parameters, which makes functions harder to test and reuse independently.
  2. Forgetting that modifying $GLOBALS["x"] inside a function actually changes the real global variable $x, not a copy -- this can cause surprising side effects.
  3. Confusing $GLOBALS with the global keyword -- global $x; imports one specific variable into a function's scope, while $GLOBALS is an array holding all of them.
Chapter Summary
  • $GLOBALS is a superglobal associative array where every key is a global variable's name and every value is that variable's current value.
  • Reading or writing $GLOBALS["varName"] inside a function reads or writes the actual global variable, not a copy.
  • Reaching for function parameters and return values is usually a cleaner design than relying on $GLOBALS for passing data around.
Browser Support

$GLOBALS has been available in every PHP version and works identically everywhere.

🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.