Broad Network


PHP Local Function Scope

Variable Scope in PHP - Part 2

Foreword: In this part of the series, I give you the basics of what is known as PHP variable scope.

By: Chrysanthus Date Published: 17 Nov 2018

Introduction

This is part 2 of my series, Variable Scope in PHP. In this part of the series, I give you the basics of what is known as PHP variable scope. You should have read the previous part of the series before coming here, as this is the continuation.

The Function Structure and Scoping
The following code has been written similarly to the ones in the previous part of the series; the variables and echo (print) statements have been written in a similar way.

Read the code:

    <?php

        $myVar = "my stuff";
        $hisVar = "his stuff";

        function myFn($aVar="my stuff")
            {
                echo($aVar);
                echo($hisVar);
                $herVar = "her stuff";
            }

        myFn();

        //echo($herVar);

    ?>

Test the code.

You should have tried the code. Note that a variable declared outside the function-block is NOT seen inside the function-block. Also, a variable declared outside the function structure can NOT be seen inside the function parentheses (you will verify that on your own).

Now remove the comment denotation in the last line and try the code again; notice that a variable ($herVar) declared inside a function block can NOT be seen outside the function block.

The global Reserved Word
You can make a variable declared outside the function structure to be seen inside the function block by using the global reserved word, as in the following code:

    <?php

        $myVar = "my stuff";
        $hisVar = "his stuff";

        function myFn()
            {
                global $myVar, $hisVar;

                echo($hisVar);
            }

        myFn();

    ?>

The global statement is typed at the top of the function block (inside). Try the code and note that a variable declared outside the function structure is seen inside the function block with the help of the global statement. The global statement can take more than one variable. The output is:

    his stuff

The $GLOBALS Superglobal Array
If you do not want to use the global reserved word, you can use the $GLOBALS superglobal array, which is an associative array. Read and test the following code, which illustrates its use:

    <?php

        $myVar = "my stuff";
        $hisVar = "his stuff";

        function myFn()
            {
                $a = $GLOBALS["myVar"];
                $b = $GLOBALS["hisVar"];

                echo($a), '<br>';
                echo($b), '<br>';
            }

        myFn();

    ?>

The output is:

    my stuff
    his stuff

Anonymous Functions
If you want a variable declared outside an anonymous function to be available inside the anonymous function, then use either of the above techniques. Try the following code:

    <?php

        $hisVar = "his stuff";

        $myFn = function ()
            {
                global $hisVar;

                echo($hisVar);
            };

        $myFn();

    ?>

The output is:

    his stuff

Functions and Included Files
Type the following code and save the file with the name, temp.php :

<?php

    //inclFn();

    function mainFn1()
        {
            echo 'mainFn1', '<br>';
        }
    include ("temp2.php");
    inclFn();
    function mainFn2()
        {
            echo 'mainFn2', '<br>';
        }

    mainFn1();
    mainFn2();

?>

Type the following code and save it with the name temp2.php in the same directory as the above file:

<?php

    mainFn1();

    function inclFn()
        {
            echo 'inclFn1', '<br>';
        }

?>

Run the temp.php script.

Note the position of the include statement, in the main script. The call inclFn() does not yet have the function defined in the included file, so it is not executed. However, mainFn2() is executed from the main file and not from the included file, since it is not defined in the included file. mainFn1() is executed in the included file, since its definition comes before the include statement.

When a file is included inside a main script, all defined functions of the file are available in the main script, from the point of inclusion, downward.

The output is:

    //undefined function inclFn()
    mainFn1
    inclFn1
    mainFn1
    mainFn2

Security Consideration
Try the following code:

    <?php

        $hisVar = "his stuff";

        function myFn($aVar="my stuff")
            {
                //global $hisVar, $herVar;

                echo $hisVar, '<br>';
                $herVar = "her stuff";
            }

        myFn();

        echo $herVar, '<br>';

    ?>

Because of the absence of any of the global techniques, $hisVar could not be seen in the function block, and $herVar could not be seen outside the function block. However, in the presence of the global statement, both variables are seen.

The problem is that when the variables are not seen, the return value is NULL, and null is a valid value in PHP.

Prevention. Just be very careful how you code global variables.

That is it for this part of the series, rendezvous in the next part.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

BACK NEXT

Comments