PHP global Keyword
last modified November 25, 2026
The PHP global keyword gives a function access to a variable from the top-level scope. Without it, a name used inside a function is a local variable, even when a global variable with the same name exists.
This article covers the declaration, reading and writing globals, shadowing, the $GLOBALS array, the difference from static, the assignment rule, and the alternatives that keep a function's dependencies visible.
Basic definitions
A global variable is declared in the top-level scope, outside any function. A function does not see it by default: inside the function, the same name refers to a local variable. The global keyword connects the two.
The statement global $name; makes the local name an alias for the global variable of that name. It is a reference, not a copy, so reading and writing the variable inside the function acts on the global value. Several names may be listed after one global, separated by commas, and the statement must appear before the variable is used in that function.
Every function that needs the variable has to declare it. The declaration applies to one function only and does not propagate to functions it calls. The $GLOBALS array is an alternative that needs no declaration. Superglobals such as $_GET or $_SERVER are already available in every scope, which is why global $_GET is pointless.
Reading a global variable
The first example uses two globals: a counter that a function increments, and a site name that another function returns.
<?php
declare(strict_types=1);
$counter = 0;
$siteName = "ZetCode";
function increment(): void
{
global $counter;
$counter++;
}
function currentSite(): string
{
global $siteName;
return $siteName;
}
increment();
increment();
echo $counter, "\n";
echo currentSite(), "\n";
increment declares global $counter and increments the global value, so two calls leave it at 2. currentSite declares global $siteName and returns it. Each function declares only the variable it uses; a single declaration in one function would not help the other one.
$ php global_basic.php 2 ZetCode
Modifying globals
Because global creates a reference, assignments inside the function are visible outside. The next example accumulates a total and swaps two values.
<?php
declare(strict_types=1);
$total = 0;
function addToTotal(int $amount): void
{
global $total;
$total += $amount;
}
addToTotal(5);
addToTotal(7);
echo "total: $total\n";
$a = 1;
$b = 2;
function swapFirstTwo(): void
{
global $a, $b;
[$a, $b] = [$b, $a];
}
swapFirstTwo();
echo "a=$a b=$b\n";
addToTotal adds each amount to the same global, so the total is 12 after two calls. swapFirstTwo declares two names in one statement and swaps them with array destructuring; the swap is visible in the top-level scope. This is exactly the kind of hidden side effect that makes globals hard to follow: the call looks like a plain statement, but it changes state elsewhere in the file.
$ php global_modify.php total: 12 a=2 b=1
A local variable with the same name
Without the declaration, a function that uses the name gets a local variable. Reading it before assigning anything produces a warning and null.
<?php
declare(strict_types=1);
$total = 5;
function readWithoutGlobal()
{
return $total;
}
function readWithGlobal(): int
{
global $total;
return $total;
}
$value = readWithoutGlobal();
var_dump($value);
echo readWithGlobal(), "\n";
function shadow(): int
{
$total = 100;
return $total;
}
echo shadow(), " ", $total, "\n";
readWithoutGlobal has no access to the outer $total, so PHP reports an undefined variable and the function returns null. readWithGlobal declares the name and returns 5. The shadow function assigns 100 to its own local variable; the outer value stays 5, and the two numbers are printed side by side to show that they are separate storage.
$ php global_scope.php Warning: Undefined variable $total in /app/global_scope.php on line 9 NULL 5 100 5
The $GLOBALS array
$GLOBALS is a superglobal array that holds every global variable, keyed by name. It is available inside functions without a global statement.
<?php
declare(strict_types=1);
$visits = 0;
$page = "home";
function trackVisit(): void
{
$GLOBALS['visits']++;
}
trackVisit();
trackVisit();
echo $visits, " visits to ", $GLOBALS['page'], "\n";
$GLOBALS['page'] = "about";
echo $page, "\n";
foreach (['visits', 'page'] as $name) {
echo "$name = ", $GLOBALS[$name], "\n";
}
trackVisit increments $GLOBALS['visits'] and needs no declaration. A global can also be changed through the array from the top level, and $page immediately shows the new value. The final loop treats the array as data: the keys are variable names and the values are their current contents. That flexibility is also a risk, because a typo in a key silently creates a new global.
$ php global_globals.php 2 visits to home about visits = 2 page = about
global versus static
A static variable also survives between calls, but it is private to the function and starts at its initial value once. A global is shared with the whole file.
<?php
declare(strict_types=1);
$calls = 0;
function countWithGlobal(): int
{
global $calls;
return ++$calls;
}
function countWithStatic(): int
{
static $calls = 0;
return ++$calls;
}
echo countWithGlobal(), " ", countWithGlobal(), " global=$calls\n";
echo countWithStatic(), " ", countWithStatic(), " ", countWithStatic(), "\n";
Both counters return 1 on the first call and 2 on the second, but countWithGlobal changes the top-level $calls, while countWithStatic keeps its state inside the function. The static counter continues to 3 on the third call and cannot be read or reset from outside. The static keyword article covers the lifetime and the restrictions of static variables.
$ php global_static.php 1 2 global=2 1 2 3
The $GLOBALS assignment rule
Individual entries of $GLOBALS can be read, added, changed, and removed. The array itself cannot be replaced.
<?php declare(strict_types=1); $GLOBALS = ['total' => 0]; echo $GLOBALS['total'], "\n";
The assignment $GLOBALS = [...] is a compile-time error: $GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax. Use $GLOBALS['total'] = 0 for a single entry. The restriction exists because the array is a view of the symbol table, not ordinary storage that can be swapped for a different array.
$ php global_disabled.php
Fatal error: $GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax in /app/global_disabled.php on line 5
Stack trace:
#0 {main}
Replacing globals with parameters
A global hides a dependency. Passing the value as a parameter, or passing an object that owns it, makes the dependency visible in the signature and lets a test supply a different value.
<?php
declare(strict_types=1);
final class Config
{
public function __construct(
public string $siteName = "ZetCode",
public int $visits = 0,
) {
}
}
function renderHeader(Config $config): string
{
return "<h1>" . htmlspecialchars($config->siteName) . "</h1>";
}
$config = new Config();
$config->visits = 3;
echo renderHeader($config), "\n";
echo "visits: ", $config->visits, "\n";
renderHeader receives a Config object instead of reaching for a global, so the function works with any configuration and the call site shows what it depends on. The readonly promoted property keeps the site name from changing after construction. Values that never change at runtime are better expressed as class constants or with the const keyword.
$ php global_alternative.php <h1>ZetCode</h1> visits: 3
Best practices
Globals are sometimes unavoidable in small scripts, but a function that depends on them is harder to reuse and to test.
Source
This article covered the PHP global keyword: declarations, modification, scope, $GLOBALS, static state, and safer alternatives. Continue with PHP variable scope for the full picture, or read PHP variables for declarations, constants, and superglobals.
Author
List all PHP tutorials.