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 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top