PHP internals at a high level

Conceptually there are a lot of differences between running a program and writing a web application through PHP. This usually comes up when a programmer has to write their first ajax request. So, lets see how stuff works on the background. A short, high level, tour through PHP internals.

How does the web work?

Obviously PHP is built for the web, so it’s restricted by how the web works. Most of the web is dictated by the hypertext transfer protocol. And the fun part is that this is a really simple protocol. I will skip the fact that DNS is somewhere in between as well. So how does http work? This is just about what happens when you type in in your browser:

Sequence diagram requesting

The stuff going on between your browser and is http. It’s basically a request, with which you can send some data, sent to the server. The server will then think a bit about what to reply, and send back a response. Once it sends this response it cannot change it in any way. The browser will interpret this response, in this case the response headers will cause the browser to render this as html.

If there happens to be an image, a style sheet or JavaScript file referenced on the page, this same schema starts all over.

How does PHP work?

When the server sees that the requested file is a PHP file, and everything is properly configured, PHP will be run. The job of PHP is to convert this request “Can I see your homepage”, into a response. One thing you need to know about PHP internals is that it runs server side, so that means that this:

Hello, world.

results in the same response as this:

function giveWorld() {
return 'world';
?>Hello, <?php echo giveWorld() ?>.

This diagram shows the PHP internals globally (and simplified):

High level image of PHP internals

So PHP will:

  • Interpret the request. This means populating the superglobals.
  • Execute your code.
  • Send the response. When the last line of the first accessed file is reached it will send all the content that hasn’t been sent yet. It’s also possible to use flush to send the response sooner. Once the response is sent however, there is no way to alter it.
  • Clean up the variables. Clears all memory (except APC) and unsets all variables.

The last step is the interesting bit: Any objects that have been made, any variables set will be removed at the end of every request. PHP will “shut down”.

The only way to get PHP to do (cool) stuff for you again is to make a new request. The initiative for this will have to come from the client side, the browser. Usually you’ll do this with a link to a new page, or by sending a form. But sometimes that isn’t good enough, sometimes you only want to refresh a part of the page. The following won’t work:

<div onclick="<?php echo functionToBeRunOnClick() ?>">click me!</div>

The function will be executed at the first request! So that is not going to work. The way you can accomplish this is by using JavaScript to do a new request to the server, this is what is called AJAX.

So this already concludes the very short, high level tour through PHP internals. To really understand this stuff, I suggest some further reading:

So why did I write this post about PHP internals?

Sometimes I get the question, how do the PHP internals look? Usually by someone just starting to learn PHP. Some other times, I get a question like “how does ajax work?”, and I end up explaining this. So perhaps mostly because I’m lazy ;-).

Leave a Reply

Your email address will not be published. Required fields are marked *