Variable Scope in PHP: A Complete Beginner’s Guide

11/19/2025
All Articles

markdown

Variable Scope in PHP: A Complete Beginner’s Guide

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.


⭐ 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 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

⭐ 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 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

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

Article