PHP Reference ยท Variables and Constants
PHP variable scope
Reference for PHP variable scope: local and global variables, the global keyword and $GLOBALS, static variables, closures with use, arrow functions and class scope.
Scope decides where a variable name is visible. In PHP, the main script has the global scope, and every function, method and closure has its own local scope. A function cannot see global variables unless you ask for them explicitly, and its local variables disappear when it returns.
The tools for crossing scopes are the global keyword, the $GLOBALS array, static variables that survive between calls, use in closures, and parameters and return values, which are the cleanest option. Blocks such as if, for and foreach do not create a scope: variables created inside them stay visible afterwards in the same function.
$rate = 0.2; function tax(float $amount): float { return $amount * $rate; // $rate is undefined here (warning) }
Try it
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.
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
global $a, $b; // bind local names to globals $GLOBALS["name"] // read or write any global static $state = 0; // keeps its value between calls function () use ($x, &$y) { } // capture from the parent scope fn($n) => $n + $x // captures by value, automatically
| Scope | What lives there |
|---|---|
| Global | Variables set in the main script (and included files at top level). Constants and functions are global in a different way: they are visible everywhere. |
| Function / method | Parameters and variables created in the body. Reset on every call. |
| Static variable | Declared with static; one value per function. Since PHP 8.1, a method inherited without being overridden shares its static variables with the parent class. |
| Closure | Only variables listed in use, plus $this when defined in a class. |
| Arrow function | Reads the parent's variables by value at creation time; cannot modify them. |
| Class | Properties and constants are reached through $this->, self:: or static::, never as bare variables. |
Usage notes
- Superglobals (
$_GET,$_POST,$_SERVER,$GLOBALS, ...) are visible in every scope without any declaration. - Prefer parameters and return values to
global. Globals make code hard to test and to reason about. includeshares the including scope. Included files at top level see globals; included from inside a function they see that function's locals.- Static initializers must be constant expressions before PHP 8.3; since 8.3 they may be any expression.
- Closures capture at creation.
use ($x)freezes the value when the closure is created;use (&$x)follows later changes. Arrow functions behave likeuseby value. unset($x)inside a function only affects the local name, even for aglobal $xalias.- Constants defined with
constordefine()are global and ignore function boundaries. See const and define. - Nested functions declared inside functions are not nested scopes; they become global functions when the outer one runs, and see none of its variables.