Confirm that PHP static variables do not persist across requests
P粉567281015
P粉567281015 2023-10-30 09:06:47
0
1
642

I'm looking for a guarantee that static variables are not stored between PHP requests. The following are previous questions:

PHP static variables across multiple .php pages

Do static variables in php persist across requests?

Static variables across sessions

To be clear they are not, but they are more about providing a way to maintain state than a concrete discussion of expected behavior.

For example, if my PHP code is as follows:

function myfunc() { static $a=0; print $a++; } for ($i=0;$i<10;$i++) myfunc();

Then every time I run it I get an output of 0123456789. My intuition/understanding of PHP makes me fairly certain that this must be the case.

In my own experiments, I shut down a (pre-forked) apache into a child process to ensure that the variable was not remembered between requests. As I would expect, it is not remembered between requests. But this is just one scenario where PHP runs.

What I'm looking for is:

Link to official documentation which says this is expected behavior and will not change. The relevant parts of the PHP documentation don't explicitly mention this (except in the comments).

Alternatively, when static variables are remembered across requests (such as a web server or a performance-enhancing PHP framework), it may not clear static variables to improve speed between requests.

P粉567281015
P粉567281015

reply all (1)
P粉654894952

PHP does not retain application state between requests. During the PHP application lifecycle, the application is re-executed on every request. Static variables are designed to retain the value of the variable in the local function scope when execution leaves the scope. Nowhere in the documentation does it mention that static variables are intended to retain values across requests.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!