How Does PHP Work With The Web Server And Browser?

Servers

Admittedly this post is less about the actual task of programming, and more about the behind-the-scenes workings of PHP's execution environment.

Introduction

PHP is a popular server-side language that is particularly good for web applications. Some of the largest companies and organizations from around the world utilize PHP for their operations (some of them you probably use every single day). A large amount of web sites and applications are powered by PHP; therefore an understanding of the PHP language is mandatory to fully understand and accept the power behind popular frameworks (such as Laravel, CodeIgniter or Symfony), and how popular websites and applications may be handling user's data.

To kick start this mini-series of posts, we will begin by looking at how users interact with the web server, and how the web server and PHP talk to one another.

The Language

PHP is an interpreted language. This means that you will write code statements (lines of code) and when a page is requested, the PHP interpreter will load your PHP code, parse it and then execute it. This differs from other languages, such as Java or C#, where the source code is compiled and then executed. This is useful for web development in the fact that you do not have to re-compile your source code for trivial code changes and the changes have immediate effect on all subsequent requests.

PHP is written as standard text files with the .php extension. PHP files are often saved within a folder in a web server's public directory (or a web root directory). On most systems this will either be named public or public_html. For example, if a file was saved as index.php in a web root directory, a user could access it by typing http://www.example.org or http://www.example.org/index.php.

public/
| index.php

PHP and Default Pages (Directory Indexes)

Most PHP and web server setups let you use a file named index.php just as you would use an index.html file. However, make sure you know which one takes priority so you don't get unexpected results (usually index.php).

The Request Life-cycle

So what exactly is happening when a user types in the URL http://example.org? When a user types in http://example.org in a Web client (a browser, for instance), the client issues a GET request to the server (let's assume that we are both using Apache). When Apache gets this request, it looks for a file named index.php (or index.html, remember the directory indexes from earlier?). If a file named index.php is found, Apache essentially says "Hey, this is a PHP file because it has the .php extension. I am going to give this to the PHP interpreter".

After Apache decides that is is a PHP file, it gives it to the PHP interpreter. When PHP receives the file it reads through it and executes any PHP code it can find. After it is done with the file, the PHP interpreter gives the output of the code, if any, back to Apache. When Apache gets the output back from PHP, it sends that output back to a browser which renders it to the screen.

PHP And Apache Output

Beginners to PHP programming might often ask questions like "How do I make an image with PHP?" or "How do I make a text box with PHP?". In all honesty, PHP does neither. In a traditional sense, the main goal of PHP is to generate some HTML document that a browser can render.

However, modern applications built with client-side MVC frameworks often see the role of PHP change to just interacting with server-side data storage.

Let's take another look at this process with a diagram. In this diagram, we will assume the user is going to the Laravel website at http://laravel.com/. The following figure has circled numbers that will highlight the various stages of the request. A step-by-step explanation of each step follows the figure.

PHP And The Apache Web Server | A quick overview of how the various servers interact during the request lifecycle.

Step 1
The user enters `http://laravel.com` into their browser and taps/hits 'enter'.
Step 2
After the user has tapped/hit 'enter', the browser sends the page request over the Internet to the web server.
Step 3
The web server gets the request and analyzes the request information. Apache realizes that we didn't specify a file, so it looks for a directory index and finds `index.php`.
Step 4
Since Apache knows to send files that end with the `.php` file extension to the PHP interpreter, it asks PHP to execute the file.
Step 5
In this step, PHP is executing the code contained in the `index.php` file from the request. During this step, PHP may interact with databases, the file system or make external API calls, amongst other things.
Step 6
After PHP has finished executing the `index.php` file, it sends the output back to Apache.
Step 7
Apache receives the output from PHP and sends it back over the Internet to a user's web browser. This is called the `web response`.
Step 8
The user's web browser receives the response from the server, and renders the web page on a computer or device.

As you can see, PHP interacts with a web server in a very real way. The actual request process is very simple, and one of the reasons that PHP is so well suited for web application development.

Conclusion

This concludes this first article in a little series about the basics of programming with PHP. The next articles will cover things such as variables, operators and functions. After that, we will take a dive into the world of Object Oriented Programing (OOP) and see how that fits in with PHP.

I hope you enjoyed this article and if you have any comments or suggestions, feel free to leave a comment below or drop a line on Twitter or Google+.

Join the Discussion

Leave a comment

  • george
    george
    July 21, 2017 3:41 PM Reply

    very helpful. Got a clear understanding

  • Abdul Barik
    Abdul Barik
    February 25, 2018 12:34 PM Reply

    Great Content! I found after lot of googing! Thanks to this writter

  • Santosh
    Santosh
    March 13, 2018 2:24 PM Reply

    But how web server know to check index.php by default what will happen if index.html exists?

  • Zhenjie Hao
    Zhenjie Hao
    March 15, 2018 11:20 PM Reply

    Wow, it's really good passage to introduce how PHP and Apache work. Love it. Thanks!

  • Doom Shop
    Doom Shop
    September 19, 2018 1:38 AM Reply

    great stuff

  • yogesh kumar
    yogesh kumar
    October 13, 2018 6:27 PM Reply

    thank you for your efforts this was really simple and helpful loved it keep doing good work. :)

Subscribe to our newsletter