PHP Static Variables: Why Doesn't It Reset?

In summary, the code is using a static variable to generate a sequence of numbers, starting from 0. It is initialized only in the first call of the function and retains its value every time the function is called. This is because static variables have a limited visibility and do not lose their value when program execution leaves their scope. The same result could be achieved by declaring the variable as global, but static variables are preferred for their limited visibility.
  • #1
topsquark
Science Advisor
Insights Author
Gold Member
MHB
2,020
827
The code
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}

myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>

is supposed to generate
0
1
2

My question is this: If we declare $x = 0 in the function then why doesn't it reset to 0 every time we run the function?

-Dan
 
Technology news on Phys.org
  • #2
Hey Dan,

A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. [m]$x[/m] is initialized only in the first call of the function and every time the myTest() function is called, it will print the value of [m]$x[/m] and increment it. :)
 
  • #3
MarkFL said:
Hey Dan,

A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. [m]$x[/m] is initialized only in the first call of the function and every time the myTest() function is called, it will print the value of [m]$x[/m] and increment it. :)
Thank you. But couldn't we do the same thing by declaring $x as a global variable?

-Dan
 
  • #4
topsquark said:
Thank you. But couldn't we do the same thing by declaring $x as a global variable?

-Dan

Yes, you could write:

PHP:
function increment_x()
{
	global $x;
	echo $x;
	$x++;
}

$x = 0;
increment_x();
echo "<br>";
increment_x();
echo "<br>";
increment_x();
 
  • #5
Ah good! That means I am actually learning something! (Dance)

-Dan
 
  • #6
For the record, a static and a global variable are the same thing as far as life time is concerned (called static duration).
The difference is that a static variable has limited visibility - it can only be accessed within the scope in which it has been defined (that is, up to the enclosing '}').
 

FAQ: PHP Static Variables: Why Doesn't It Reset?

Why do static variables in PHP not reset?

The reason static variables in PHP do not reset is because they are only initialized once and retain their value throughout the lifetime of the program. This means that even if the function or method in which the static variable is declared is called multiple times, the variable will not be reset.

How does a static variable differ from a regular variable in PHP?

A static variable differs from a regular variable in that it is not destroyed when the function or method in which it is declared ends. This allows the variable to retain its value and be accessed again when the function or method is called again.

Can a static variable be reset in PHP?

Yes, a static variable can be reset in PHP by assigning a new value to it within the function or method in which it is declared. However, this goes against the purpose of using a static variable, which is to retain its value throughout the lifetime of the program.

How can I reset a static variable without assigning a new value?

There is no way to reset a static variable without assigning a new value to it. However, you can use a regular variable instead of a static variable if you need its value to be reset each time the function or method is called.

Are there any advantages to using static variables in PHP?

Yes, there are several advantages to using static variables in PHP. They can improve performance by reducing the need to re-initialize variables, and they can also be used to share data between different functions or methods without the need for global variables.

Similar threads

Replies
13
Views
2K
Replies
6
Views
6K
Replies
13
Views
1K
Replies
7
Views
5K
Replies
2
Views
3K
Replies
15
Views
2K
Replies
10
Views
9K
Back
Top