tags:

views:

193

answers:

5

It seems that a static variable declared in a function is re-initiated whenever the function is called, how can I use the function in a way that re-calling the function will re-use the static parameter?

I defined the function 'testStatic' in static.php

here is static.php:

<?php
function testStatic()
{
    static $staticV = 0;
    echo $staticV;
    $staticV;
}
?>

I am calling 'testStatic' from index.php

here is index.php:

<?php include "./static.php";?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3c.org/TR/html4/strict.dtd"&gt;
<?php
testStatic();
?>

<html> 
.
.
.
<html>

When index.php is executed for the first time testStatic will echo with '0' however in the next times index.php is executed testStatic continues to echo with '0'. It seems that the static variable 'staticV' of 'testStatic' is re-initiated whenever index.php is execute.

please advise. that index.php

+5  A: 

Every time you execute a PHP script, the environment is re-created. There is no state between HTTP requests or script calls.

The first time you point your web browser at index.php, a new PHP environment is initialized, and $staticV becomes 0.

The next time you point your web browser at index.php, the exact same thing happens.

If you want to persist $staticV between web requests, then you need a different approach. Sessions are often used to handle this problem.

zombat
+4  A: 

HTTP is a stateless protocol, so no state is maintained on each call to a web page. The variable will be static, only for the duration of the PHP processing of index.php, the next time you load index.php it will not have remembered any state from the previous time you ran index.php, including the static variable.

To do that you will need to use some method of data persistence. Store the variable in a file, a database, as a session variable, etc. Storing it in a session is probably the best option here. There's a tutorial on sessions if you need it.

A quick example,

<?php
session_start();

if(isset($_SESSION['staticV']))
    $_SESSION['staticV']++;
else
    $_SESSION['staticV'] = 0;

echo $_SESSION['staticV'];
?>
Rich Adams
+1  A: 

hmmmm variables in php are volatile in what concerns different calls. every time you start a php-script, the variables are reset. if you want persistence over multiple calls, you need to use a persistent storage like a file or a database. sessions / cookies are a way, too.

regards

Atmocreations
+3  A: 

you are confusing things. static variable stays static within (!) the script execution. once the script finished, php engine doesn't know anything about the variables or the script...

you should use cookies or database (or php session options) to keep values during session.

dusoft
+1  A: 

This occurs because every time the page executes all old information (other than what is stored in superglobals like $_SESSION) are reset. This includes the function, which gets defined anew every time. The code you are using would do what you expected if you were to execute it multiple times in the generation of that page. Separate page loads can essentially be considered separate instantiations of the program, so you would need to use an alternative location to store the information. DB, text file, a superglobal... Choose one that suits your purpose and use that for the information.

Be aware that if you want this number to be in sequence not for a particular user, but for all users, you will have to do extra work to handle synchronization.

krdluzni