Admittedly this post is less about the actual task of programming, and more about the behind-the-scenes workings of PHP's execution environment.
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.
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_html. For example, if a file was saved as
index.php in a web root directory, a user could access it by typing
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
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.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.
- 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.
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+.