The PHP Variables Scope is like a boundary. The scope defines, where a variable can be accessed in the code.

Logically, there are two different variable scopes in PHP:

  1. local
  2. global

Local Sope in PHP

When you create or declare a variable inside a function it is called a local variable, and this variable only accessible inside the function.

<?php
function myFunc(){
    // Local Scope
    $x = "Apple";
    echo $x;
}
myFunc();

/*
you can't do this -

echo $x;

Because this variable has a local scope,
it will only be accessible inside the function.
*/
?>
Browser Output
Apple

Global Scope in PHP

In PHP, the global scope is the opposite of the local scope. When you create or declare a variable outside a function, that variable has the global scope.

You can’t access this variable inside a function. It is accessible only outside a function.

<?php
// global scope
$a = "Hello";

function myFunc(){
    /* you can't do this -

    echo $a;
    
    You can’t access the $a variable inside a function
    because it has the global scope. */

    echo "Inside Function";
}
myFunc();

echo "<br>Global Scope - $a";
?>
Browser Output
Inside Function
Global Scope – Hello

PHP static keyword

Normally, after a function is fully executed, all its variables are removed. And when you call this function again, it will recreate or redeclare the variables again.

<?php
function myFunc(){
    // Declaring the $x variable whose value is 0
    $x = 0;
    // Increasing the $x. Now $x = 1;
    $x++;
    // Showing the $x
    echo $x;
}
myFunc();
echo "<br>";
myFunc();
echo "<br>";
myFunc();
?>
Browser Output
1
1
1

But, sometimes we want the local variable not to be deleted. To do this, the PHP static keyword helps us.

When you use the static keyword, the local variable will not be removed after calling a function. And when you recall the function it will use the old variable.

<?php
function myFunc(){
    // Static variable declaration
    static $x = 0;
    // Increasing the $x by 1
    $x++;
    // Showing the $x
    echo $x;
}

/* it will show 1
$x = 0;
$x++; */
myFunc();
echo "<br>";

/* it will show 2
$x = 1;
$x++; */ 
myFunc();
echo "<br>";

/* it will show 3
$x = 2;
$x++; */ 
myFunc();
?>
Browser Output
1
2
3

The global keyword

If you want to access the local variable outside a function or you want to access the global variable inside a function, the global keyword helps for this.

Accessing local variables outside a function

<?php
function myFunc(){
    /* first, you have to declare the variable
    with the global keyword */
    global $x;
    $x = "Local Variable";
    echo "Inside of the function - $x";
}

myFunc();

echo "<br>Outside of the function - $x";
?>
Browser Output
Inside of the function – Local Variable
Outside of the function – Local Variable

Accessing Global variables inside a function

<?php
$x = "Global Variable";

function myFunc(){ 

    /* you have to declare the variable
    with the global keyword */   
    global $x;
    echo "Inside of the function - $x";
}
myFunc();

echo "<br>Outside of the function - $x";
?>
Browser Output
Inside of the function – Global Variable
Outside of the function – Global Variable

Leave a Reply

Your email address will not be published. Required fields are marked *