Saturday, November 12, 2011

How PHP Works

how php works

Summary: In this tutorial I will explain you how PHP works, what happens when you type in a PHP file URL, how server handles PHP files, what PHP interpreter do, how PHP connects to databases, how the processed PHP code is send back to the browser and lastly there is a video tutorial on How PHP works in both English and Hindi.

How PHP Works

PHP scripts are interpreted on the web server and the result is send back to the client machine, so to run any PHP script you need a web server with PHP installed and then you type in the URL of PHP file on browser window.

What happens when you execute a PHP Script

Whenever you type in the URL of the file containing PHP Scripts in your browser window, browser sends an HTTP request to the web server, say you are typing the URL of the file Contact.php and hit enter, then browser will send the request to server and server will start finding this file on its file system. If the file is found by the server, server will send this file to PHP interpreter otherwise the server will generate ERROR 404, which is also known as file not found.

A point to be noted is that Server sends only those files to the PHP interpreter whose file extension is .php , all other files such as .html, .htm and others are not send to PHP interpreter even if they contain PHP codes inside them.

What is the task of PHP Interpreter

Once the file is send to PHP interpreter, it starts finding all of the opening and closing PHP tags and then process the PHP code inside these tags.


 // opening and closing php tags


PHP interpreter also checks that whether there is a database connection or not, if it founds database connection then it will send or retrieve data from the database. Once this process is complete, it sends the result back to the client machine.

How PHP Works [Video tutorial in English]

How PHP Works [Video tutorial in Hindi]

This is the second chapter in which I explained How PHP Works, in the next chapter I will explain you How to install PHP in your local machine.