Updated on May 9, 2018
To explain how PHP executions work, we also have to provide you with the way web servers operate on a very basic level. This will help you understand how web servers interact with the php interpreter in order to execute a php script.
As we all know, the Web Server is part of the Web Hosting Server's Software Layer. As such it is running as a service (process, daemon) on the Web Hosting Server implementing the Client-Server model of communication making its sole purpose to answer web requests made to websites hosted on the server. Typically, these requests are sent by the Web Browser of a visitor to a website hosted on the Web Hosting Server.
The whole process can be visualized with the following diagram:
How the Web Server process PHP scripts?
Primarily the Web Servers are intended of serving only static files such as:
... by directly sending those as a response to request made by the visitor's browser. In other words the Web Server is not configured to directly serve dynamic language files such as .php files.
In fact, if a website owner place .php script on a vanilla Web Server installation the file will be interpreted and delivered in Plain Text format meaning that all the PHP code will be delivered as a response in the visitor's Web Browser.
So how does the Web Server know what result to display when a PHP script is requested by the Web Browser of a website visitor?
To answer that question we need to go deeper into the PHP Ocean. In order for a PHP script to be executed as a standalone programming entity, an interpreter is needed. The interpreter is a binary script which is being executed only when called. So, if a PHP script needs to be executed, the interpreter is being called either by the user directly via the console of the server or via some other service already running on the Web Hosting Server (the Web Server).
In the context of PHP the following command line example can be considered when there is a PHP interpreter installed on the server:
root@fastcomet [~]# whereis php php: /usr/bin/php root@fastcomet [~]# echo "<?php echo 'Hello World!' ?>" > helloworld.php root@fastcomet [~]# /usr/bin/php -q helloworld.php Hello World!
The same approach is valid in the Web Server context - when a request to a PHP script hits the Web Server (for example https://exmaple.com/index.php), it is being directed by the Web Server to the PHP interpreter available on the Web Hosting Server. The interpreter basically executes all the programming logic implemented in the PHP file and returns the result to the Web Server in either Plain Text or HTML format. From that point on, the Web Server considers that result as a Static resource and returns it to the Web Browser of the visitor who made the request.
The whole process can be visualized with the following diagram:
With this we can conclude our tutorial on how PHP executions work. For information about the reasons for increased number of PHP executions on your hosting account and the best practices for reducing the usage of this resource, please check the corresponding tutorials.