Broad Network


Static Variables in PHP

Variable Scope in PHP - Part 3

Foreword: In this part of the series, I explain static variables.

By: Chrysanthus Date Published: 17 Nov 2018

Introduction

This is part 3 of my series, Variable Scope in PHP. In this part of the series, I explain static variables. You should have read the previous parts of the series before coming here, as this is the continuation.

Static Variable
Try the following code and note that the variable, $herVar declared inside the function block and made global, can be seen outside the function, but cannot be changed:

    <?php

        function myFn()
            {
                global $herVar;
                $herVar = "her stuff";
                echo $herVar, '<br>';
            }

        myFn();
        echo($herVar), '<br>';
        $herVar = "their stuff";
        myFn();
        echo($herVar), '<br>';

    ?>

The output is:

    her stuff
    her stuff
    her stuff
    her stuff

At the moment, with or without the global feature (reserved word), you cannot change a variable declared inside the function block outside the block. However, it is possible to change the variable, still inside the block, by making it static; and with a function call. Read and test the following code:

    <?php

        function myFn($aVar)
            {

                static $herVar = "her stuff";

                if ($aVar == "my stuff")
                    $herVar = "their stuff";
                echo $herVar, '<br>';
            }

        myFn("Oh my");
        echo($herVar), '<br>';
        myFn("my stuff");

    ?>

The output is:

    her stuff
    
    their stuff

The result show that the static variable cannot be seen outside the function block, but it lives on after the function has been executed. Some code within the block can change it, for each function call.

If the static variable is not initialized at declaration, its initial value will be NULL.

Static Variable in Recursive Function
Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion. The following simple function recursively counts from 1 to 20, using the static variable $a to know when to stop:

<?php

    function fnRecu()
        {
            static $a = 0;

            $a = $a + 1;
            echo $a, ' ';

            if ($a < 20)
                fnRecu($b);
        }

    fnRecu();

?>

The output is:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

The Global and Static Variables
A global variable declared inside the function block, can be seen outside the function block, but cannot be changed outside the block.

A global variable declared outside the function block, can be seen inside the function block, if the reserved word, global is used. The same variable, seen inside and outside the function block, can be changed both inside and outside the function block. Try the following code:

    <?php

        $herVar = "my stuff";

        function myFn()
            {
                global $herVar;

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

        myFn();
        $herVar = "his stuff";
        myFn();

    ?>

The output is:

    my stuff
    her stuff
    his stuff
    her stuff

A static variable declared inside a function block, lives on after the function has been executed, but it cannot be accessed from outside the function.

You do not declare static variable outside a block.

That is it for this part of the series.

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

Comments