PHP $GLOBALS
In this page:
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
$siteName = "Cookies Cursor";
print_r($GLOBALS['siteName']);
?>
Login to try C/C++/Java/PHP code in the editor
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
$siteName = "Cookies Cursor";
function showSite() {
echo $GLOBALS['siteName'];
}
showSite();
?>
Login to try C/C++/Java/PHP code in the editor
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
$counter = 0;
function increment() {
$GLOBALS['counter'] = $GLOBALS['counter'] + 1;
}
increment();
echo $counter;
?>
Login to try C/C++/Java/PHP code in the editor
$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
$siteName = "Cookies Cursor";
function withGlobalKeyword() {
global $siteName;
echo $siteName . "\n";
}
function withGlobalsArray() {
echo $GLOBALS['siteName'];
}
withGlobalKeyword();
withGlobalsArray();
?>
Login to try C/C++/Java/PHP code in the editor
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
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
?>
Login to try C/C++/Java/PHP code in the editor
- Overusing $GLOBALS as a way to avoid passing values into functions as proper parameters, which makes functions harder to test and reuse independently.
- Forgetting that modifying $GLOBALS["x"] inside a function actually changes the real global variable $x, not a copy -- this can cause surprising side effects.
- 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.
- $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.
$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: