Variable Scope In PHP: In Brief

Share Now

Variable scope is a boundary for a variable within which it can be visible or accessed from code.

There are only two scopes available in PHP namely local and global scopes.

  1. Local variables (local scope)
  2. Global variables (special global scope)
  3. Static variables (local scope)
  4. Function parameters (local scope)
function printHW(){
    global $hw; //global scope
    $name = "nahid"; //local scope
    echo $hw;

function printHW2(){
    echo $GLOBALS['hw']; //$GLOBALS -> Super Global Array


function doSomething() {
    static $i;//static scope
    $i = $i?? 0;
    echo $i;
    echo "\n";
Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert