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 variabl开发者_Go百科es 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:
- Request is sent to server, whether by user, ajax, curl through PHP or what-have-you
- Relevant PHP script is executed, whether as a module on your web server, a CGI worker process, or other options
- Script is executed, response to the request (if any) is created and sent
- (optional) script continues to execute some other job until eventual termination, at which time all its variables die with it.
The "PHP run" is always from start of execution untill the end of the script. So, if you call a PHP script with ajax or a PHP calls another PHP via a header(), each call is a single run. The static variables instantiated earlier do not have a persistant state and will be redefined.
Either static variables or not...if you want to have a persistent state of data throughout those requests you will either have to save it in the session, cookie, database, or in a cache.
It depends on the server setup. Typically, when you make a request, the PHP interpreter is loaded, parses the script, your server spits out the results and the interpreter is destroyed. This happens for each request, regardless of whether it originates from a user browsing or AJAX. What this means is that "static" variables are only valid until the interpreter is destroyed, which again, is at the end of every request. (HTTP is stateless)
What do you mean by "PHP calling more PHP via a header()"? Are you referring to a redirect? In that case, it's a new request. If you meant "PHP calling more PHP via an include", it's typically not a new request (the edge case being you are including a PHP script from a 3rd party.. dangerous and not recommended). With an include, PHP simply loads and executes the file in the same context as the originating script.
The PHP static (that could be considered "global" in a procedural way) doesn't persist in any of your cases.
In each of them a new HTTP request is performed and the Php variables state is lost.
In the command line there is a defined start and end.
There is no difference in PHP running on a server. When a web request is made to the script, the script runs till the end of the script, or until it crashes or has a time-out (and possibly other similar issues).
AJAX doesn't run server side. AJAX is another client side asynchronous call to a server resource. Everything that's done for the first request as far as authentication, validation, input checking, etc has to be done with every subsequent request. The difference in an AJAX response is that the PHP script is likely to return only the content that's requested.
The only time a program will "persist" is if it has been told to keep going. PHP can be told to wait, and perform actions via web sockets, but that seems to be outside the scope of your question.
All three are the same.
In each case, the user's browser is making an http request for the url. The runtime is from the time the server receives the request to the time it is fullfilled.
The PHP scripts stops when you exit, reaches the end of the script or fails.
精彩评论