Variable Scope in PHP: A Complete Beginner’s Guide

11/19/2025

markdown

Go Back

Variable Scope in PHP: A Complete Beginner’s Guide

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.


markdown

⭐ What Is Variable Scope?

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.


⭐ Types of Variable Scopes in PHP

PHP supports four main scopes:

  1. Local Scope

  2. Global Scope

  3. Static Scope

  4. Super Global Variables

Let's understand them one by one.


⭐ 1. Local Scope

A variable declared inside a function is local to that function.

It cannot be accessed outside the function.

Example:

<?php
function test() {
    $x = 10; // Local variable
    echo $x;
}

test();        // Output: 10
echo $x;       // Error: Undefined variable
?>

⭐ 2. Global Scope

A variable declared outside a function has global scope.

Global variables cannot be accessed inside functions directly.

Example (Incorrect):

<?php
$x = 50;

function demo() {
    echo $x;  // ❌ Error: Undefined variable
}

demo();
?>

✔ Correct Way: Use the global keyword

<?php
$x = 50;
unction demo() {
    global $x;
    echo $x; // Output: 50
}

demo();
?>

⭐ Using $GLOBALS Array

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.


⭐ 3. Static Scope

A static variable inside a function retains its value even after the function ends.

Example:

<?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.


⭐ 4. Super Global Variables

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

Most Common Super Globals:

Super Global VariableDescription
$_POSTHandles form data sent via POST
$_GETHandles query parameters
$_SERVERContains server information
$_FILESFile upload data
$_COOKIECookie values
$_SESSIONSession values
$_REQUESTCombined GET, POST, COOKIE
$GLOBALSAll global variables

⭐ Example of Super Global

<?php
echo $_SERVER['HTTP_HOST'];
?>

⭐ Practical Example: Using Global + Local + Static

<?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();
?>

⭐ Common Mistakes

❌ 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)


⭐ Summary Table of Variable Scopes

Scope TypeDeclared Where?Accessible Where?Requires Keyword?
LocalInside functionOnly inside that functionNo
GlobalOutside functionEverywhere except inside functionsYes (global)
StaticInside functionOnly inside function, retains valueYes (static)
Super GlobalsSystem-definedEverywhereNo

Conclusion

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.


SEO Meta Description

Learn variable scope in PHP with examples. Understand local, global, static, and super global variables in this beginner-friendly PHP programming guide.

SEO Meta Keywords

php variable scope, php local variable, php global variable, php static variable, php superglobals, php programming basics, php tutorial for beginners