PHP variables persist while the program is running... just like any other programming language.
Now, how long does the program actually run? We have to distinguish between:
- The PHP interpreter itself (written in C)
- Your script (written in PHP)
The PHP interpreter is not designed to share variables between scripts so it doesn't really matter how long it runs.
Your script will run, well, until it's finished: it reaches the last line of finds an exit
, die()
or return
statement. This simple fact should already answer your question. But it's also worth taking into account that there's no point in keeping a PHP script running continuously like a desktop application when it's only used to serve an HTTP request. Unlike other protocols (namely FTP or IRC), HTTP is stateless: each user request starts and closes the connection*.
(*) That's not entirely true (connections can and are normally reused) but doesn't affect the design implications.