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 '}').
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top