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

Click For Summary
The discussion centers on the behavior of static variables in PHP, specifically regarding the function `myTest()`, which uses a static variable `$x`. When the function is called, `$x` retains its value across calls, starting from 0 and incrementing with each call, which contrasts with a regular variable that would reset each time. Participants clarify that static variables maintain their value within the local function scope, while global variables can be accessed throughout the script. Although both types of variables have a similar lifespan, static variables are limited in visibility to their defining scope, unlike global variables, which can be accessed anywhere in the script. This distinction highlights the importance of understanding variable scope and lifetime in PHP programming.
topsquark
Science Advisor
Homework Helper
Insights Author
MHB
Messages
2,020
Reaction score
843
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
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. :)
 
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
 
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();
 
Ah good! That means I am actually learning something! (Dance)

-Dan
 
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 '}').
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
1K
  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 33 ·
2
Replies
33
Views
7K
  • · Replies 7 ·
Replies
7
Views
5K