Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

PHP global and static keywords

Reference for PHP global and static in functions: reach globals from a function, keep a static variable between calls, and the limits of both.

Variables inside a PHP function are local: a function cannot see the variables of the code that called it, and its own variables vanish when it returns. Two keywords change that. global binds a local name to a variable of the global scope. static gives a function a variable that keeps its value between calls.

Both are used inside function bodies. (static also has other meanings, for static class members and static closures, covered in static properties and methods.)

PHP
function visits(): int
{
    static $count = 0;
    return ++$count;
}

Try it

PHP global versus passing arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP static variables keep their value

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
global $name1, $name2;
static $name = initial_value;
static $a = 1, $b = [];
Name Description
global $name Creates a local reference to $GLOBALS['name']. If the global does not exist, it is created with the value null.
static $name = value; Declares a function-level variable initialized once. Since PHP 8.3 the initializer may be any expression; before that it had to be a constant expression.

Return value: neither is an expression; they are declarations.

Errors: none specific. Using global at file level has no effect.

Usage notes

  • global is a reference. Assigning to the local name changes the global. unset($name) on it only removes the local alias.
  • $GLOBALS reads or writes any global by key without a declaration; since PHP 8.1 you can no longer overwrite the whole array. See $GLOBALS.
  • Avoid globals. They hide dependencies, make code hard to test and cause surprises in included files. Pass arguments and return values, or hold shared state in an object.
  • Statics live per function. Methods share one static between all instances of a class (as in the example where $a and $b count together). Since PHP 8.1, a method inherited by a child class shares its static variables with the parent's method.
  • Closures. Each closure object has its own static variables, so two closures created from the same code count separately.
  • Initialization. The value is computed at the first execution of the static line (for non-constant initializers since 8.3), and later calls skip it.
  • Typical uses: counters, memoization caches (memoFib above), one-time setup. For anything larger, a class property is easier to reason about and to reset in tests.
  • Reset. A static variable is not reset by unset(), only by ending the script. Long-running workers keep it between requests.