Categories

Articles in Basics Category

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.

PHP can also be used as a language for Command line Scripting and Desktop applications. But it’s widely used for web applications.

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 Normal Web Request

Cycle of a Web Request That Involves PHP

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.

Don’t worry if you don’t understand the code yet. Try to understand accompanying explanations.

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.

As stated, only the output is sent to the client. So, no PHP content can be seen by viewing the source of PHP files in client’s web browser.

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.

&lt;?php
if (date('G') &lt; 12) {
    $greeting = 'Good Morning!';
} else {
    $greeting = 'Welcome!';
}
?&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to Our Web Site&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;&lt;?php echo $greeting; ?&gt;&lt;/h1&gt;

&lt;p&gt;Rest goes here...&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;

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.

&lt;?php

if (date('G') &lt; 12) {
    $greeting = 'Good Morning!';
} else {
    $greeting = 'Welcome!';
}

echo '&lt;html&gt;';
echo '&lt;head&gt;';
echo '&lt;title&gt;Welcome to Our Web Site&lt;/title&gt;';
echo '&lt;/head&gt;';
echo '&lt;body&gt;';

echo &quot;&lt;h1&gt;$greeting&lt;/h1&gt;&quot;;

echo '&lt;p&gt;Rest goes here...&lt;/p&gt;';

echo '&lt;/body&gt;';
echo '&lt;/html&gt;';

?&gt;

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.

Where to Head from Here...
Share with Your Peers...

We Value Your Feedback...

We love to hear what you think about this article. Please provide your opinion, suggestions and improvements using following form. Note that submitted feedback is not displayed but we will get back to you if it needs a reply.