PHP脚本什么时候结束?

In my mind, a web app something that runs continuously; therefore, I'm confused by documentation pages that talk about the "end" of a PHP script (eg this one). Such references seem to refer to the end of each web request, but if the script ends there, doesn't that mean that the OS has to setup a whole new process for each request? That seems unlikely, because spinning up a whole new process is expensive, and be very inefficient for the whole site.

There's a distinction to make here: PHP is not a web server. Therefore, the end of a PHP script is irrelevant to the actual end of the web server (or at least, most of the time).

Web server software that continuously run, like IIS, Apache or Lighttpd can execute PHP scripts without ending themselves. These scripts, at the end, do cease to run. How web servers set up the execution environment for the script depends on the approaches taken. For instance, Apache 2 can indeed spawn a child process for each web request, though it does not set up again the whole web server each time: there's a master process that commands the children about execution settings and parameters.

Therefore, a PHP script ends when the web request ends; that is, when the script reaches a statement that makes it stop (like a fatal error, an exit statement or the like) or when it reaches the actual end of the script source.


EDIT About the efficiency of spawning a new process for new requests.

Nowadays, servers don't spawn a separate process for PHP itself: they use the PHP library to embed the language and work with it. However, servers are indeed free to spawn a process for every HTTP request they receive. Apache does it with its mpm_prefork module.

While spawning a new process is indeed expensive, it's potentially nothing when compared to the cost of the script itself, which can last tenths of seconds, which is much, much more than just spawning a process.

Also, servers usually have to serve many clients at once; therefore, it is unthinkable to just treat every request sequentially. They must be run concurrently, and that leaves implementors two options: threads or processes. PHP not being thread-safe on many platforms, it is often preferable to run it in a separate process.

Yep, this is what actually happened, each request. This is the nature of PHP.