Variable & its Scope in PHP

Variable Scope:

In PHP, variables can be declared anywhere in the script.

The scope of a variable is the part of the script where the variable can be referenced/used.

So there are basically three variable scopes in PHP, which are:

  1. Local – Any variable declared within a function.
  2. Static – Any local variable that can live out of its scope. Similar to local variables, static variable too can only be used within a function.
  3. Global – Any variable that can be accessed from anywhere.

Let’s check out the below PHP code first and later we can go with further explanation on the variable scopes.

$y = 5; // global variable
    testAll();
    testGlobalAndStatic(); // Call 1
    testGlobalAndStatic(); // Call 2
    function testAll() {
        $x = 8; // local variable
        global $y; // accessing a global variable
        static $z = 10; // static variable
        echo "<p><strong>testAll function</strong>: Variable x is a local variable and it's value is: $x</p>"; //8
        echo "<p><strong>testAll function</strong>: y is a global variable and it's value is: $y</p>"; // 5
        echo "<p><strong>testAll function</strong>: z is a static variable and it's value is: $z</p>"; // 10
        $z++;
        static $z = 10; // this statement never applies, as $z has already been declared as static
        echo "<p><strong>testAll function</strong>: The value of static variable z now is: $z</p>"; // 11
        $z = 10;
        echo "<p><strong>testAll function</strong>: The value of static variable z now is: $z</p><br>"; // 10
        $y = $y + 1; // can also be written as $y++
    }<span id="mce_marker" data-mce-type="bookmark" data-mce-fragment="1">​</span><span class="crayon-h"></span>
 function testGlobalAndStatic() {
        $a = 99;
        static $b = 99;
        global $y; // accessing a global variable
        echo "<p><strong>testGlobalAndStatic function</strong>: y is a global variable and it's value is: $y</p>";// First call - 6, Second call - 6
        echo "<p><strong>testGlobalAndStatic function</strong>: a is a local variable and it's current value is: $a</p>"; // First call - 99, Second call - 99
        echo "<p><strong>testGlobalAndStatic function</strong>: b is a static variable and it's current value is: $b</p>";// First call - 99, Second call - 100
        // Now incrementing both local variable a and static variable b
        $a++;
        $b++;
        echo "<p><strong>testGlobalAndStatic function</strong>: The value of local variable a after incrementing by 1 is: $a</p>";// First call - 100, Second call - 100
        echo "<p><strong>testGlobalAndStatic function</strong>: The value of static variable b after incrementing by 1 is: $b</p><br>";// First call - 100, Second call - 101
    }<span id="mce_marker" data-mce-type="bookmark" data-mce-fragment="1">​</span>

 

Try to understand each and every line of the PHP code and its output above.

Below are few important points to note:

  • A local variable scope is only within a function, which means they can be declared and accessed within the same function. Therefore as soon as the function execution ends, all its local variables get destroyed and are no more accessible.
  • Static variables are similar to local variables, except that static variables hold the values even after the function execution gets completed, which means they are not destroyed after the function execution completes. This can be clearly understood from the above code snippet, where we are calling the testGlobal And Static function twice one after the other.

So this is for today, in our next tutorial we will learn about Data Types in PHP. Stay connected with us for more tutorials.

If you like this tutorial please share it with your friends, colleagues and with your relatives, keep visiting and be a part of the Tekraze family. If you have any problem with this tutorial please comment in the comment box and if you have any suggestion regarding this then please tell us.