Variable Scope in PHP: A Complete Beginner’s Guide
markdown
Understanding variable scope is crucial in PHP because it determines where a variable can be accessed or modified. PHP has different types of scopes that control the visibility of variables inside functions, scripts, and files.
This guide explains all types of variable scopes with clear examples: local, global, static, and super global variables.
Variable scope refers to the area within a PHP script where a variable can be accessed.
In PHP, variables may not always be accessible everywhere—they follow specific rules depending on where they are declared.
PHP supports four main scopes:
Local Scope
Global Scope
Static Scope
Super Global Variables
Let's understand them one by one.
A variable declared inside a function is local to that function.
It cannot be accessed outside the function.
<?php
function test() {
$x = 10; // Local variable
echo $x;
}
test(); // Output: 10
echo $x; // Error: Undefined variable
?>
A variable declared outside a function has global scope.
Global variables cannot be accessed inside functions directly.
<?php
$x = 50;
function demo() {
echo $x; // ❌ Error: Undefined variable
}
demo();
?>
global keyword
<?php
$x = 50;
unction demo() {
global $x;
echo $x; // Output: 50
}
demo();
?>
Another way to access global variables inside functions.
<?php
$x = 10;
$y = 20;
function sum() {
echo $GLOBALS['x'] + $GLOBALS['y'];
}
sum(); // Output: 30
?>
$GLOBALS is a super global array storing all global variables.
A static variable inside a function retains its value even after the function ends.
<?php
function counter() {
static $count = 0;
echo $count;
$count++;
}
counter(); // Output: 0
counter(); // Output: 1
counter(); // Output: 2
?>
Static variables are useful for counters, caching, and maintaining states.
PHP provides built-in variables that are always accessible anywhere in the script.
These are called super globals, and they are available:
inside functions
outside functions
in all scopes
| Super Global Variable | Description |
|---|---|
$_POST |
Handles form data sent via POST |
$_GET |
Handles query parameters |
$_SERVER |
Contains server information |
$_FILES |
File upload data |
$_COOKIE |
Cookie values |
$_SESSION |
Session values |
$_REQUEST |
Combined GET, POST, COOKIE |
$GLOBALS |
All global variables |
<?php
echo $_SERVER['HTTP_HOST'];
?>
<?php
$siteName = "OrientalGuru"; // Global variable
function showDetails() {
global $siteName; // Access global variable
static $visit = 1; // Static variable
echo "Welcome to $siteName - Visit #$visit <br>";
$visit++;
}
showDetails();
showDetails();
showDetails();
?>
❌ Trying to access local variables outside a function
❌ Forgetting to use global keyword
❌ Not understanding that static variables preserve values
❌ Misusing super globals without validation (security risk)
| Scope Type | Declared Where? | Accessible Where? | Requires Keyword? |
|---|---|---|---|
| Local | Inside function | Only inside that function | No |
| Global | Outside function | Everywhere except inside functions |
Yes (global) |
| Static | Inside function | Only inside function, retains value |
Yes (static) |
| Super Globals | System-defined | Everywhere | No |
Understanding variable scope is essential for writing secure, clean, and bug‑free PHP code. Mastering local, global, static, and super global variables will help you build more structured and efficient applications.
Learn variable scope in PHP with examples. Understand local, global, static, and super global variables in this beginner-friendly PHP programming guide.
php variable scope, php local variable, php global variable, php static variable, php superglobals, php programming basics, php tutorial for beginners