How long do PHP static variables persist, i.e. how long does a “PHP run” persist? With say a command line program there is a defined start and end, but in web w/ AJAX I don’t know how to define this.
Here are 3 ways I’ve seen a PHP script started.
- User (Requesing a PHP page)
- Javacript calling PHP (AJAX)
- PHP calling more PHP via a header()
In my actual application I have javascript call a php script via AJAX that script uses the header() to reload the site. This would be consideredt two different runs. Each has their own static variables that do not relate.
PHP variables persist for the lifetime of the script running through the interpreter. In the case of a web request, this is the lifetime of handling the requests. Your three cases are all requests to a server, and thus are handled the same: the static variables survive until the script terminates after handling the request.
The life span of PHP (and its variables) over a request: