PHP variable scope decides where in your code a variable can be read or changed. Variables created inside a function are local to that function, variables created outside any function are global, and variables declared with static keep their value between calls of the same function. Unlike many languages, a PHP function cannot see global variables unless you ask for them.
This guide explains the four scope situations you meet every day: local, global, static and function parameters, plus the safer alternatives to the global keyword.
A variable assigned inside a function exists only while that function runs. When the function returns, the variable is gone, and the next call starts fresh.
Two functions can use the same variable name without affecting each other, because each call has its own local scope.
A variable defined at the top level of a script, outside any function, is in the global scope. PHP does not make it available inside functions automatically.
In PHP 8 this raises a warning for the undefined variable $rate, and the result is treated as null, so the output is 0. The function has its own empty scope. Note that code in included files runs in the scope of the line that included it.
You can import a global variable into a function with the global keyword, or read and write it through the $GLOBALS superglobal array.
Both approaches work, but both hide a dependency: a reader of the function cannot tell from its signature that it changes outside state. That makes code harder to test and debug. Since PHP 8.1, you can no longer write to the whole $GLOBALS array or pass it by reference, although reading and writing a single element such as $GLOBALS['counter']++ still works.
A static variable is local to a function but is initialised once and keeps its value between calls.
Static variables are useful for counters and small caches. Remember that they live for the length of the request only. Since PHP 8.1, a static variable in an inherited method is shared between the parent and child class. For anything shared across requests use a database, a cache or the session.
The cleanest way to give a function data is to pass it as a parameter and return the result.
If a function must change a caller's variable, pass it by reference with &:
Anonymous functions do not see the surrounding variables either, unless you capture them. A normal closure captures with use, by value or by reference. An arrow function (fn, PHP 7.4) captures the parent scope by value automatically.
Changes made to a by-value captured variable inside the closure do not affect the outside variable. Use use (&$total) when you need that.
| Kind | Declared | Lives | Visible in functions |
|---|---|---|---|
| Local | Inside a function | One call | Only that call |
| Global | Top level | Whole request | Only via global or $GLOBALS |
| Static | static inside a function | Whole request | Only that function |
| Parameter | In the signature | One call | Only that call |
Loops, if blocks and try blocks do not create their own scope in PHP. A variable first assigned inside a for loop is still available after the loop.
PHP functions have their own scope. Pass the value as a parameter, capture it with use, or import it with global if you must.
Usually yes, because it hides dependencies and makes testing hard. Prefer parameters, return values, objects and dependency injection.
No. Only functions, methods, closures and arrow functions create a new variable scope, plus the file top level.
A static variable is visible only inside its function but keeps its value between calls. A global variable lives at the top level and is shared by the whole script.
PHP has local, global and static variable scope, and functions do not see globals by default. Prefer parameters and return values, use static for small per-function state, capture variables in closures with use, and avoid the global keyword in new code.