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>'; 
?>

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;
		}
}

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.

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();

?>

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.