Guide to PHP Variable Scopes By Examples

Variable Scope PHP

The variable scope PHP is defined as the range of conditions in which it can be accessed within a given program. There are three different sorts of variable scopes in PHP:

To get in-depth knowledge in Variable Scope PHP: PHP Training in Chennai provides necessary training with experienced PHP developers.

1. Local Variable in PHP

In the next statement, “local variables” refer to variables specified within a function. A local variable in PHP has just the scope of the function in which they are declared. Because these variables are exclusively accessible within the function, they cannot be accessed outside of it.

The declaration of a variable with the same name outside of a function is fundamentally different from the declaration of a variable with the same name inside a function. Using the following example, let’s get a better understanding of local variables:

File: local_variable1.php

<?php  

    function local_var()  

    {  

        $num = 35; //local variable  

        echo “Local variable declared inside the function is: “. $num;  

    }  

    local_var();  

?>  

Output:

Local variable declared inside the function is: 35

File: local_variable2.php

<?php  

    function mytest()  

    {  

        $lang = “Java”;  

        echo “Web Programming language: ” .$lang;  

    }  

    mytest();  

    //using $lang (local variable) outside the function will generate an error  

    echo $lang;  

?>  

Output:

Web programming language: JAVA

To get proper guidance to familiar with PHP programming language: PHP Online Course at FITA Academy is the ideal platform to get best training. They will make you an expert at where are you are.

2. PHP Global Variable

These variables can be accessed at any time from any location. In a function, the GLOBAL keyword should be used before the variable name. These variables can be accessed or used directly outside of the function, if necessary. It is not necessary to use a keyword in order to access a global variable outside of the function. An illustration can assist you in comprehending PHP global variable:

Instant:

File: global_variable1.php

<?php  

    $name = “Harsh Vardhan”; //Global Variable  

    function global_var()  

    {  

        global $name;  

        echo “Variable inside the function: “. $name;  

        echo “</br>”;  

    }  

    global_var();  

    echo “Variable outside the function: “. $name;  

?>  

Output:

Variable inside the function: Harsh Vardhan

Variable outside the function: Harsh Vardhan

Instant:

File: global_variable2.php

<?php  

    $name = “Harsh Vardhan”; //global variable  

    function global_var()  

    {  

        echo “Variable inside the function: “. $name;  

        echo “</br>”;  

    }  

    global_var();  

?>  

Output:

Notice: Undefined variable: name in D:\xampp\htdocs\program\p3.php on line 6

Variable inside the function:

3. Static Variable PHP

PHP deletes the variable after execution and memory are released. Even after a function has finished, we need to store a variable. The static variable is thus a crucial aspect of variable scoping. To define a static variable, we use the static keyword before the variable name.

Static variables are only available in local functions and do not free memory when the application exits. An example will help you understand:

Instant:

File: static_variable.php

<?php  

    function static_var()  

    {  

        static $num1 = 2; //static variable  

        $num2 = 4; //Non-static variable  

        //increment in non-static variable  

        $num1++;  

        //increment in static variable  

        $num2++;  

        echo “Static: ” .$num1 .”</br>”;  

        echo “Non-static: ” .$num2 .”</br>”;  

    }  

      

//first function call  

    static_var();  

  

    //second function call  

    static_var();  

?>  

Output:

Static: 3

Non-static: 6

Static: 3

Non-static: 6

You should take note of the fact that $num1 increases in value after each function call, however, $num2 does not increase. This is due to the fact that $num1 is not a static variable, and as a result, it cleared its memory after each function call was completed.

Finally, if you plan to get training in PHP language, Step into PHP Training in Coimbatore for the immense training.

    Quick Enquiry