php programming
php programming

Introduction

This article aims to clarify a few of the basic points about using PHP to create dynamic HTML pages. Before reading it from start to finish, the reader should be aware that there are some pre-requisites. Luckily, there are a selection of articles right here to help with the basic techniques that should be understood:
  • HTML Tutorial – for those new to HTML;
  • Programming Concepts & Principles – for those new to programming.
Essentially, the reader should know about flow control, using variables and condition testing (if…then…endif constructs) in computer programming. In addition, a working knowledge of basic HTML structure and style would be useful.

Embedded PHP in HTML Design

One of the easiest ways to use PHP is within an existing HTML document. This is a good way to get started, since the existing legacy HTML can be leveraged during the learning process. If we assume that the HTML programmer wishes to add some PHP code to display a simple message, we could use code such as:
<HTML>
<BODY>
<?php
echo 'Hello from PHP!';
?>
</BODY>
</HTML>
The above code will insert the phrase Hello from PHP! into the HTML flow, and it will be rendered using the style currently in force. In other words, all that the PHP software has done is interpret the code, and replace it with the resulting HTML. Things to note:
  • PHP code blocks are contained within <?php and ?gt;;
  • The keyword echo is used to output plain text.
While this is not immediately useful, it is important for the understanding of related PHP topics that the reader remembers these two points.

Splitting PHP Blocks in HTML Documents

Assuming for a moment that we have a variable $motd containing the current system ‘message of the day‘, and another $motd_severity, containing a value which details the importance of the current MOTD:
  • High
  • Medium
  • Information
We would like to change the presentation (HTML) of the MOTD, depending on the ‘severity‘. Red text for High, green text for Medium, and blue text for Information. We could also conditionally add a little icon, or any other HTML element. There are two ways to do this, depending on the complexity of the HTML we wish to include. We can either create a piece of PHP and echo each HTML statement into the text stream, or we can break out of the PHP code block to selectively insert a piece from the actual HTML file into the text stream. This sounds a little counter-intuitive at first, but the following example should help to make the second option clearer:
<HTML>
<BODY>
<?php
if ($motd_severity == 'High') {
?>
<FONT COLOR=Red>
<?php
} elseif ($motd_severity == 'Medium') {
?>
<FONT COLOR=Green>
<?php
} else {
<FONT COLOR=Black>
}
// end of if block
?>
<?php echo $motd; ?>
</FONT>
</BODY>
</HTML>
This example could probably have been better served by using the standard inline PHP syntax, because the formatting is not very complex. However, as soon as one starts adding icons, styles and so forth to the HTML, it quickly becomes easier to stick with HTML blocks inside PHP rather than PHP blocks inside HTML. The modes can be mixed, however, as the last PHP statement shows, so there is real flexibility on offer. The final way that we can write PHP and HTML is to include all the HTML inside a PHP code block. To do this, we make a file (usually named .php) that contains the <?php at the start, and ?> at the end. Between these two tokens, we should write only PHP code, resulting in a correctly formed piece of HTML. This is useful if the PHP file is to be included as a server side include. Written this way, our first example would look like:
<?php
echo '<HTML>';
echo '<BODY>';
echo 'Hello from PHP!';
echo '</BODY>';
echo '</HTML>';
?>
That is really all that there is to using PHP and HTML together – PHP gives the flow, and HTML the presentation.