Static Variables in PHP with Example
Static variables in PHP are those variables that retain the values even after the function exits. In other words, variables which hold their values between multiple function calls are called static variables.
When the execution of a function is completed, its local variable gets destroyed, and the memory is freed. However, if you need that value again for further task, you won’t be able to retrieve it unless you use a static variable.
Generally, a variable defined inside the block of a function holds its initial value. It does not hold the updated value once the function exits. So, if you have multiple function calls in a PHP program, it will always display the initial value of that variable instead of updated or the latest value.
To retrieve this updated value, you need to place a static keyword before the variable inside the function. A static variable is initialized only once and retains its value even after the function exits.
Syntax to Define Static Variables in PHP
To define a static variable in PHP, you need to place STATIC or static keyword in the front of the variable inside the block of a function. The general syntax to define a static variable inside the function is as follows:
function functionName() {
static $variableName = initialValue;
// Function code
}
Example 1:
<?php
function counter() {
static $count = 0; // Static variable.
$count++; // Increment by 1.
echo $count;
}
// Calling the function multiple times.
counter();
echo "\n";
counter();
echo "\n";
counter();
?>
Output: 1 2 3
In this example, we have defined a counter() function that contains a static variable named $count. This variable is declared with a static keyword. Initially, it is set with a value 0.
Each time the function is called, the statement $count++; increments the value of $count variable by 1. After that, the value of $count retains its value during multiple function calls. The statement echo $count; then displays the current value of $count.
When we have called the counter() function first time, $count is initialized to 0 and then incremented to 1. The echo statement outputs 1. During the second function call, the static variable $count retains its previous value of 1, and is incremented to 2 by the statement $count++. The echo statement now outputs 2.
On the third call, the static variable $count holds the previous value 2 and is again incremented to 3. Now the echo statement will display the value 3.
Each time the function is called, the static variable keeps its value from the previous function call instead of being re-initialized. Thus, when you call the function multiple times, it always displays the latest or updated value of the variable every time.
Advanced Example of Static Variable
Let’s take an advanced example in which we will understand the difference between the local variable and static variable inside the function. This example code will help to understand how a local variable gets re-initialized every time the function is called, while a static variable retains its value across multiple function calls.
Example 2:
<?php
function normalFunc() {
$a = 10; // local variable.
$b = 20; // local variable.
$a = $b + $a;
echo "Latest value of local variable a = ", $a;
}
function staticFunc() {
static $x = 10; // static variable.
$y = 5;
$x = $y + $x;
echo "Latest value of static variable x = ", $x;
}
normalFunc();
echo "\n";
normalFunc();
echo "\n";
normalFunc();
echo "\n";
staticFunc();
echo "\n";
staticFunc();
echo "\n";
staticFunc();
?>
Output: Latest value of local variable a = 30 Latest value of local variable a = 30 Latest value of local variable a = 30 Latest value of static variable x = 15 Latest value of static variable x = 20 Latest value of static variable x = 25
In this example, we have defined two functions named normalFunc and staticFunc. Inside the normalFunc() function, we have defined two local variables named $a and $b, respectively. Both variables hold the integer values.
Then, we have used the arithmetic operator (+) to sum the $a and $b and the resultant value is assigned to the variable $a with the help of assignment operator (=).
Similarly, inside the staticFunc() function, we have defined one static variable named $x and another local variable named $y. Both variables hold the integer values of 10 and 5.
After that, we have called the normalFunc() function three times, which display the same value 30 every time on the console. However, when we have called the staticFunc() function three times, it displayed the updated value of $x every time.
Scope of Static Variables
In PHP, static variables have a local scope, which means that they are not accessible outside the block of a function in which they are declared in. Let’s take an example on it.
Example 3:
<?php
function staticVarScope() {
static $myVar = 10; // local scope.
}
// Trying to access the static variable outside the function.
echo $myVar; // Error: Undefined variable $myVar
?>
Always remember that static variables are limited to the scope of the function in which they are defined. Unless explicitly declared as global variables using global keyword, you cannot access these variables globally. However, static variables inside a function remain local unless you do not explicitly make it global, which is generally not recommended for clean code practices.
In this tutorial, you learned about static variables in PHP with the help of basic examples. I hope that you have understood the basic syntax to define static variables and practiced all programs.