Understanding PHP Variables Scope
In PHP, variables have a certain scope that determines where they can be accessed and used within the code. This scope is determined by the location where the
The scope of a variable is the part of your program where that variable is visible and can be used. In PHP, scope is decided by where you declare the variable: a variable created at the top level of a script is global, while a variable created inside a function is local to that function. Crucially — and this surprises people coming from JavaScript or Python — PHP functions do not automatically see variables from the surrounding script. Each function gets its own private set of variables.
This page covers the three scopes you need in everyday PHP — global, local, and static — plus the two ways to reach a global value from inside a function (global and $GLOBALS), and when to prefer passing arguments instead.
Local Variables
A variable assigned inside a function has local scope: it exists only while that function runs and is invisible everywhere else. This is the default and the scope you should reach for most often, because it keeps functions self-contained and predictable.
<?php
function myFunction() {
$x = 10; // local to myFunction()
echo $x; // works: prints 10
}
myFunction();
echo $x; // Warning: Undefined variable $x
?>The second echo fails because $x never existed outside the function. The reverse is also true: a $x defined in the main script is not visible inside myFunction() unless you explicitly bring it in. Local variables are ideal for counters, intermediate results, and anything temporary you don't want leaking into the rest of the program.
Global Variables
A variable declared at the top level of a script — outside any function — has global scope. Despite the name, it is not automatically available inside functions. To read or write it from within a function you must pull it into scope with the global keyword.
Without the global $x; line, the $x inside the function would be a separate, undefined local variable. The global keyword links the local name to the global one, so changes inside the function affect the value outside it too.
The $GLOBALS array
PHP also stores every global variable in a built-in superglobal array called $GLOBALS, indexed by variable name. Because superglobals are visible in every scope, this gives you a second way to reach a global without the global keyword:
<?php
$x = 5;
$y = 10;
function addNumbers() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addNumbers();
echo $z; // Outputs 15
?>Here $z is created in global scope from inside the function. $GLOBALS is handy when you need several globals at once, but it has the same downsides as global.
Use globals sparingly
Globals create hidden, action-at-a-distance dependencies: any function can change them, so a bug becomes hard to trace. Prefer passing values in as arguments and returning results, which keeps each function's inputs and outputs explicit:
<?php
function addNumbers($a, $b) {
return $a + $b;
}
echo addNumbers(5, 10); // Outputs 15
?>Reach for global or $GLOBALS only for genuinely program-wide configuration, not as a shortcut around passing data.
Static Variables
Normally a local variable is destroyed when the function returns, so it starts fresh on the next call. A static variable is the exception: declared with the static keyword, it keeps its value between calls while still staying local (invisible outside the function). It's perfect for remembering state across calls — for example, counting how many times a function has run.
The static $x = 0; initializer runs only on the first call; on later calls PHP skips it and reuses the stored value.
Static variables are declared using the static keyword and are only accessible within the function where they are declared.
Conclusion
In conclusion, variables in PHP have a scope that determines where they can be accessed and used. Understanding the different types of scopes, such as global, local, and static, can help you write more efficient and effective code. By using the appropriate scope for your variables, you can ensure that your code is organized and easy to maintain.