How Web Servers Process PHP Code

When a web server receives a request for a PHP page, the server processes the PHP code before sending the response.

A typical PHP page, consisting of one or more code islands, works like this:

  • The server creates a buffer (area in memory for temporary storage).
  • The server analyzes the PHP document from the top down, dividing the PHP code islands from blocks of static HTML
  • When the server encounters a block of static HTML, it appends that block to the buffer
  • When the server encounters a PHP code island, the server processes the PHP code in that island
  • Dynamic content generated by the PHP code (if any) is appended to the buffer
  • When all of the PHP code is complete, and all dynamic content appended to the buffer, and all of the static HTML appended to the buffer, then the entire contents of the buffer becomes the response which is sent to the user