PHP novice on the road

web front end five thousand one hundred and ninety-six 13 years ago (2011-04-04)

Many features of PHP are related to other software or tools. Using the PHP knowledge we have learned so far, we can try to build a simple interactive website. We can learn a lot from this process. Well, we are now focusing on the construction of a typical personal website.  


5.1 Planning a site

Generally, a personal site includes a welcome page, a message book page, a bookmark link page, a counter, contact information, and even a photo album and some music files.  

5.2 Modularization with include and require

Let's look at some websites that use PHP architecture. Almost every page of the website will have include and require embedded in the PHP file. This is because the use of include and require can not only enhance the readability of the code, but also divide the site into modules for management. Generally speaking, every page on a website is bound to have duplicate content. For example: navigation bar and advertisement in the page header Icon , or edge navigation, etc. There may also be copyright or some text-based navigation bars in the footer of each page, that is, at the bottom of the page. If we want to modify the navigation bar or logo on a large website containing thousands or hundreds of pages, we can only change each page one by one using our previous methods. Needless to say, you can also imagine what a difficult and painful job it is. So, do we have a better solution? The answer is yes. We can put the repeated content into a file, and then use the include and require functions of PHP to dynamically call the file on each page that needs the content. In this way, if we want to modify these reused content on all pages in the future, we only need to change the file containing these duplicate content.

To facilitate your understanding, let's first look at a simple application of include and require:

At the beginning of the HTML page, you may include it (head. htm) at the head of each page of the site.
<HTML>
<HEAD>
<TITLE>My homepage</TITLE>
</HEAD>
<BODY> 

Page content (content. htm).  
<H1>
Welcome to my humble abode, although there is nothing here at present.  
</H1> 

End of HTML page (trail. htm)
</BODY>
</HTML>

Use the include and require functions to separate HTML from PHP and divide HTML and PHP into modules:
<?
/*
Call the header of the HTML page
*/
require("head.htm");
/*
Call the content of the HTML page
*/
require("centent.htm");
/*
Call the end of the HTML page
*/
require("trail.htm");
?>

5.3 Let's start with a title page, a contact information page and a resume page. We also need standard, generic page headers and bottoms.  

Title page -- front.htm
Here we have a very simple HTML file:
<HTML>
<HEAD>
<TITLE>
My personal homepage -- Welcome
</TITLE>
</HEAD>
<BODY>
<H1>
My personal homepage
</H1>
<H2>
welcome
</H2>
<HR>
<P>
Welcome to my humble abode, although there is nothing here at present.
</P>
<P>
But I hope we can get more soon.
</P>
<HR>
<P ALIGN="CENTER">
<SMALL> <I>
Copyright ? Myself, 1999
</I> </SMALL>
</P>
</BODY>
</HTML>

Contact Information Page -- count.htm
We also have a simple page:
<HTML>
<HEAD>
<TITLE>
My personal homepage -- contact information
</TITLE>
</HEAD>
<BODY>
<H1>
My personal homepage
</H1>
<H2>
Contact Information
</H2>
<HR>
<P>
You can contact me at 1-800-PHP-INFO
</P>
<HR>
<P ALIGN="CENTER">
<SMALL> <I>
Copyright ? Myself, 1999
</I> </SMALL>
</P>
</BODY>
</HTML>


5.4 From HTML to PHP

As you can see from the above, every page has the same head and bottom. As above, it is OK to write the same information on each page when the workload is less, but imagine how much effort you will spend when there are more than 100 pages and you need to change all their heads or bottoms? What a tedious and boring thing it is to make manual changes page by page! So we should write PHP header and bottom files for these pages, and then we just need to reference them in each HTML page. The include and require functions contain a file of PHP code. No matter what the extension of the file is, it is regarded as a PHP file. We will place these include files in a subdirectory called include and take them as files with the suffix. inc. Now we will write the general content of these sites into the file.

General variable setting of the whole station: common.inc
<?
//General variable of the whole station
$MyEmail = toyean@msn.com ;
$MyEmailLink = "<a href="mailto:$MyEmail">$MyEmail</a>";
$MyName = "PHP Talk";
$MySiteName = $MyName. "'s Home Page";
?>

General page header: header.inc
<?
//Define common page headers
?>
<HTML>
<HEAD>
<TITLE>
<? echo "$MySiteName - $title"; ?>
</TITLE>
</HEAD>
<BODY>
<H1>
<? echo "$MySiteName"; ?>
</H1>
<H2>
<? echo "$title"; ?>
</H2>
<HR>

General page bottom: footer.inc
<?
//General Page Bottom
?>
<HR>
<P ALIGN="CENTER">
<SMALL> <I>
Copyright ? by 
<? echo "$MyName ($MyEmailLink)"; ?>
, 1999
</I> </SMALL>
</P>
</BODY>
</HTML>

New page front PHP3:
<?
include("include/common.inc");
$title = "Welcome";
include("include/header.inc");
?>
<P>
Welcome to my humble abode, although there is nothing here at present.
</P>
<P>
But I hope we can get more soon.
</P>
<?
include("include/footer.inc");
?>

New count PHP3:
<?
include("include/common.inc");
$title = "Contact Information";
include("include/header.inc");
?>
<P>
You can contact me at 1-800-PHP-INFO
</P>
<?
include("include/footer.inc");
?>

Now you can appreciate the benefits of this arrangement. If you want to change the head or bottom of the page, you only need to change the corresponding file. If you want to modify your e-mail address or even your name, just modify the common.inc file. It is also worth noting that you can include files with any file name or file extension in your files, and you can even include files from other sites.