954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

static variable got initialized everytime

My static variable got initialized everytime the function is called. Here's my code(simplied), thanks. -

<script>
function validate() {
		return true;
	}
</script>

<?php 
vote();
Function vote () {
	static $testvar = 1;
	echo $testvar;
	if ($testvar == 1) {
		$testvar++;
		echo $testvar;
		}
}
echo '<td><input type="image" width=50% src="pix/camera.gif" name="submitb" value="'.$tripid.'" onClick="validate()"></td>'; 
?>
coboldeveloper
Newbie Poster
7 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

move the static variable declaration outside of the function. you are telling it to change it back to 1 every time as it sits.
static $testvar = 1;

function vote () {
	echo $testvar;
	if ($testvar == 1) {
		$testvar++;
		echo $testvar;
		}
}
kireol
Posting Whiz
313 posts since Mar 2008
Reputation Points: 34
Solved Threads: 51
 

Thanks. I suspected that too, but according to my book (Learning PHP & MySQL by R'Oreilly) and some websites such as http://www.php.net/manual/en/language.variables.scope.php , http://webmaster-forums.code-head.com/showthread.php?t=202 , it seems that PHP only initializes static variables the first time this function is called, eg from one website is -

<?php
function test()
{
static $a = 0;
echo $a;
$a++;
}
?>
Anyway I've already tried but the variables defined outside of the function were not able to be referred to from inside the function.

coboldeveloper
Newbie Poster
7 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Thanks. I suspected that too, but according to my book (Learning PHP & MySQL by R'Oreilly) and some websites such as http://www.php.net/manual/en/language.variables.scope.php , http://webmaster-forums.code-head.com/showthread.php?t=202 , it seems that PHP only initializes static variables the first time this function is called, eg from one website is -

<?php function test() { static $a = 0; echo $a; $a++; } ?> Anyway I've already tried but the variables defined outside of the function were not able to be referred to from inside the function.


This doesnt do what yer looking to do? Maybe I'm not understanding your question then

<?php
    static $testvar = 1;

function vote () {
    global $testvar;
    echo $testvar;
    if ($testvar == 1) {
        $testvar++;
        echo $testvar;
        }
}


vote();
echo "<BR>";
vote();

?>
kireol
Posting Whiz
313 posts since Mar 2008
Reputation Points: 34
Solved Threads: 51
 

Solved. Thanks for your effort.

The problem was that, when the form was submitted, the php script was executed from the beginning again, instead of just calling the vote function, so the static variable got initialized again.

coboldeveloper
Newbie Poster
7 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You