Separating HTML from PHP PHP

The last approach I want to discuss involves using the include and require functions. As you may recall , these functions include a file in the PHP code. The file is considered to be a PHP file, regardless of the extension on the name. If PHP code appears in the included file, it is surrounded in <? and ?> tags. You may want to turn back to the functional reference to refresh yourself on the differences between include and require , but they aren't particularly important to this discussion.

Certain chunks of HTML must appear on every well-formed page. Additionally you may develop repeating elements such as a company logo. Rather than write them into every page, you may choose to put them into a file and dynamically include them. It contains HTML you might include at the top of every page on a site. The opening and closing code to form a complete page.

Start of HTMLPage

<HTML>
<HEAD>
<TITLE>PHP</TITLE>
</HEAD>
<BODY>

End of HTML Page

</BODY>
</HTML>

Page Content

This is the body of the page.<BR>
It's just a bit of HTML.<BR>

Page-Building Script

<?
/*
** include code to open HTML page
*/
require("20-7.html");
/*
** include content
*/
require("20-9.html");
/*
** include code to close HTML page
*/
require("20-8.html");
?>

In this way, HTML and PHP are separated into modules. In this example I have hardcoded the inclusion of a two-line HTML file, but I could just as easily have included the color tables. The HTML can be reused from page to page, and if I need to add something to every page on the site, I need to edit only that one file. I might want to add the PHP function. It will then be available for use inside the code.

It may occur to you that this approach is exhibiting another pattern. Every page on the site will simply become three calls to require. The first and last calls will always be the same. In fact every page on the site will vary simply by the name of the file included in the second require statement. This takes us beyond the issue of integrating HTML and PHP and into the structural design of a site. It is possible to create a site that has exactly one PHP script.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics