PHP Static Variables: Why Doesn't It Reset?

  • Context: MHB 
  • Thread starter Thread starter topsquark
  • Start date Start date
  • Tags Tags
    Php Static Variable
Click For Summary

Discussion Overview

The discussion revolves around the behavior of static variables in PHP, specifically why a static variable does not reset to its initial value each time a function is called. The scope of the conversation includes technical explanations and comparisons with global variables.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions why the static variable $x does not reset to 0 with each function call, despite being initialized in the function.
  • Another participant explains that a static variable retains its value between function calls and is only initialized once during the first call.
  • A participant suggests that a similar effect could be achieved using a global variable instead of a static variable.
  • Another participant confirms that both static and global variables share the same lifetime but differ in visibility, with static variables being limited to their defining scope.

Areas of Agreement / Disagreement

Participants generally agree on the behavior of static variables, but there is a discussion about the potential use of global variables as an alternative, indicating some differing perspectives on variable scope and usage.

Contextual Notes

The discussion does not resolve the implications of using static versus global variables in different contexts, leaving open questions about best practices and potential pitfalls.

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 '}').
 

Similar threads

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