How PHP works?

Monday, February 11, 2013

PHP is a server side scripting language that is used for building Dynamic Websites. It is one of the most popular Open Source scripting language that is getting better with its later versions compared to its older versions and other commercial server side languages available out there.

The most famous website Facebook was developed in PHP and still using PHP, except that they are converting the PHP code executed to C++ code for performance. They call it HiphopPHP. You might want to check about HiphopPHP on Github.

Let's get to the point on how PHP works? Above is the slide show that i made to show you guys how PHP works.

Normal PHP Working

Normally when a user or the client machine requests a page to the web server the server responds to the request by sending the requested page to the client machine. This process is simple if it is a normal HTML page. What if the requested page is a dynamically generated page like PHP. In that case the web server again requests that particular page following these steps.

  1. User requests the .php webpage to the web server.
  2. The server identifies the page to be a PHP page and it requests the PHP script engine to parse the requested PHP code.
  3. The server then sends the parsed output HTML page to the client.
  4. Finally the HTML is parsed and shown as a webpage in the client's browser.

Working of PHP with Database

Another case is that when a requested page needs information to be retrieved from the database. In that case the PHP script engine will do the work for us, but of-course we should write the code for connecting to the database. That process is show in the third slide of the above presentation.

For this kind of requests step 1 and 2 is same as shown above.

  1. The server then connects to the database with credentials provided in the PHP script.
  2. Query's the database and retrieves the data from the database
  3. Finally the PHP parser converts the data in a HTML representation and sends it to the client's browser.