Role of PHP in Web Applications
PHP is a server side scripting language. That means its processing happens in the server by consuming server’s resources and sends only the output to the client. In a client side scripting language like JavaScript, processing happens in the client’s computer consuming its resources.
To understand the role of PHP, let’s look at how a normal web request and a web request that involves PHP happen. Please note that there can be many intermediate steps involved but only the main and important ones have been mentioned for understanding purposes.
Cycle of a Normal Web Request
Cycle of a Web Request That Involves PHP
Dynamic Content
You might have probably heard that “using PHP you can create dynamic web sites”. To understand what “dynamic” means here, let’s take a look at welcome.html and welcome.php.
welcome.html
<html> <head> <title>Welcome to Our Web Site</title> </head> <body> <h1>Welcome!</h1> <p>Rest goes here...</p> </body> </html>
welcome.php
<html> <head> <title>Welcome to Our Web Site</title> </head> <body> <h1> <?php if (date('G') < 12) { echo 'Good Morning!'; } else { echo 'Welcome!'; } ?> </h1> <p>Rest goes here...</p> </body> </html>
No matter what’s the current time of the day, if you request welcome.html, you would always see Welcome! as the heading. But if you request welcome.php before 12 noon, you would see the heading as Good Morning! and Welcome!, if it is after 12 (date() function uses server’s time and it may be different from your PC’s time).
Here welcome.html has a static behavior because it delivers same content always. But welcome.php has a dynamic behavior because the content it delivers changes according to the time of the day. You can improve this file and have different greetings for different time periods. As illustrated in this example, PHP can be used to build dynamic content that is based on the context. You would experience the real benefits of PHP’s dynamic behavior when you create database driven web applications.
How PHP Engine Outputs Content
When PHP interpreter reads a file, it only processes lines between <?php and ?> tags. It outputs rest of the lines without any processing.
For an example, in welcome.php, PHP interpreter outputs all the HTML content till <?php tag without any processing. Then based on the time of the day it adds Good Morning! or Welcome! to the output. Then from </h1> tag, it outputs rest of the lines without processing. Web server collects all these outputs and sends to the client who made the request.
You can have more than one block of PHP content in a page and variables you define in these blocks are available to next blocks. For an example, below PHP file (welcome-new.php) behave as same as welcome.php.
<?php if (date('G') < 12) { $greeting = 'Good Morning!'; } else { $greeting = 'Welcome!'; } ?> <html> <head> <title>Welcome to Our Web Site</title> </head> <body> <h1><?php echo $greeting; ?></h1> <p>Rest goes here...</p> </body> </html>
Embedding PHP in HTML
When we mix PHP and HTML content in a PHP file as we did in welcome.php and welcome-new.php we call it as “embedding PHP in HTML”. We could achieve the same behavior with following PHP file (welcome-latest.php) which contains only PHP content.
<?php if (date('G') < 12) { $greeting = 'Good Morning!'; } else { $greeting = 'Welcome!'; } echo '<html>'; echo '<head>'; echo '<title>Welcome to Our Web Site</title>'; echo '</head>'; echo '<body>'; echo "<h1>$greeting</h1>"; echo '<p>Rest goes here...</p>'; echo '</body>'; echo '</html>'; ?>
Embedding PHP blocks in HTML let us get the help of PHP only where it is necessary. As you can see in these welcome files, we only need PHP to output the correct greeting. We can add rest of the lines to the output by just keeping them outside of PHP blocks instead of outputting each of those lines using PHP. This saves PHP processing resources and lets us have clean and readable files in our development.