Variable Scope in PHP
Understand local, global, and static scope in PHP functions.
Jessica Lee
October 27, 2025
5.3k245
Scope defines where a variable can be used.
Local scope
function test() {
$x = 10;
echo $x;
}
Global scope
$x = 5;
function show() {
global $x;
echo $x;
}
Static variables
function counter() {
static $count = 0;
$count++;
echo $count;
}
counter(); // 1
counter(); // 2
Next: Superglobals like $_GET and $_POST.
#PHP#Scope#Beginner