Broad Network


Static Variable in PHP

Understanding Variable Scope in PHP – Part 2

Forward: In this part of the series, we look at static variable in PHP.

By: Chrysanthus Date Published: 10 Aug 2012

Introduction

This is part 2 of my series, Understanding Variable Scope in PHP. In this part of the series, we look at static variable in PHP. Everything in this series concerns PHP 5.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Existence of Local Scope Variable
Consider the following program:

<?php

    function fn()
        {
            $e = 5;
            echo $e;
            //other statements involving $e in this block.
        }

    fn();

?>

We have a function and the variable, $e inside the function. This variable is not found anywhere outside the function. Know that when execution of this function is taking place, the variable $e exists. As soon as execution of the function is over, the variable $e (and its value) ceases to exist; that is the variable is not retained after the execution of the function; but the program carries on. This is true for all functions and their local variables.

Making Variable exist after Function Execution
Note that the variable above is a local function variable. There is a way to make a local variable as the one above, exist after function execution. To achieve that you precede the variable in its initialization in the local scope, with the keyword, static. The following program illustrates this:

<?php

    function fn()
        {
            static $e = 5;
            echo $e . "<br />";
            $e = $e + 2;
        }

    fn();
    fn();
    fn();

?>

In the program, you have the function definition and then the function is called three times. The first statement in the function initializes the local variable, $e, preceded by the keyword, static. This statement makes the variable, $e and whatever value its acquires, exist after the function has executed. The second statement echoes whatever value $e has to the browser. The third statement increases the value of $e by 2.

The function is called three times. The first time it is called, the value of $e is 5. The value of 5 is echoed and then the value of $e is increased by 2 to 7. As the execution of the function ends $e retains the value, 7 because in its initialization, it was made static. The second time the function is called, the initialization is not looked into again because it is static and is looked at only in the first function execution. In the second execution (call), the $e value is echoed as 7. Before the end of the second execution, the value $e is increased from 7 to 9. At the end of the second function execution the value of 9 is retained. In the third execution, the initialization is not looked into and the value of $e is displayed as 9. At the end of the third execution the value of $e is 11, but since the function is not called again, this value retained is not displayed.

So when a variable is initialized as static, its final value in the execution of the function is retained. If the function is called again, its initialization is not looked into.

Relationship between Static Variable and Scope
Do not forget that the static variable remains in the local function scope and does not go into the global scope. However, there is some similarity between the way the global variable behaves and the way the static variable behaves. When a global variable is initialized, it will only stop to exist without lost of its previous value when the program ends because it can be used anywhere in the program. When a static variable is initialized, it will exist at each time the function is called, without lost of its previous value. If the function is called repeatedly through out the program, that will go to demonstrate that the static variable last as long as the global variable.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course

Comments

Become the Writer's Fan
Send the Writer a Message