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

AI Thread 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 '}').
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top