PHP Novice on the road (v)

Source: Internet
Author: User
Many of the features of PHP are related to other software or tools. Using the knowledge we've learned so far, we can try to build a simple interactive web site. We can learn a lot by using this process. Well, we're starting to focus on the construction of a typical personal website.





5.1 plans a site





General A personal site includes a welcome page, a message This page, a bookmark link page, a counter, contact information, even photo album and some music files and so on.





5.2 is modular with include and require





we look at some PHP-framed sites, and almost every page of the Web site contains include and require embedded in PHP files. This is because using include and require not only enhances the readability of your code, but it also allows you to manage your site into modules. In general, there is certainly a duplication of content for each page on a Web site. For example: the page Head navigation bar, the advertisement icon, or the side navigation and so on. You may also have copyright or some text-based navigation bars on each page's footer, which is the bottom of the page. If we were to modify a navigation bar or a logo on a large web site that contained hundreds of pages, we could only make a couple of changes to each page in our previous approach. Needless to say, everyone can imagine what a difficult and painful task it is. So, do we have a better solution? The answer is yes. We can put the duplicate content in a file, and then call the file dynamically on every page that needs it with PHP's include and require functions. This way, if we want to make changes to all of these reusable content on all pages, we just need to change the file that contains the duplicate content.





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





HTML page starts, maybe you will include it in the head of each page of the site (head.htm).


<HTML>


<HEAD>


<TITLE> My personal homepage </TITLE>


</HEAD>


<BODY>





page content (content.htm).


<H1>


Welcome to my humble abode, although there is nothing here for the time being.


</H1>




End of
HTML page (trail.htm)


</BODY>


</HTML>





uses the include and require functions to separate HTML from PHP, dividing HTML and PHP into modules:


;?


/*


calls the header of an HTML page


*/


require ("head.htm");


/*


calls the contents of an HTML page


*/


require ("centent.htm");


/*


calls the tail 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 to


</TITLE>


</HEAD>


<BODY>


<H1>


My personal homepage


</H1>


<H2>


Welcome


</H2>


<HR>


<P>


Welcome to my humble abode, although there is nothing here for the time being.


</P>


<P>


But I hope I can get up more soon.


</P>


<HR>


<p align= "CENTER" >


<SMALL> <I>


Copyright? My own, 1999
.

</I> </SMALL>


</P>


</BODY>


</HTML>





Contact Information page--count.htm


also we 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 through 1-800-php-info


</P>


<HR>


<p align= "CENTER" >


<SMALL> <I>


Copyright? My own, 1999
.

</I> </SMALL>


</P>


</BODY>


</HTML>








5.4 from HTML to PHP





from above you can see that each page has the same head and bottom. Like the above, every page writes the same information, but it's OK to do it at less work, but imagine how much effort you would take to have more than 100 pages and you need to completely change the head or bottom? How tedious it is to make a page-by-page manual change! So we should write PHP headers and bottoms for these pages, and then we'll just have to reference them in each HTML page. A file containing a PHP code in the include and require functions, regardless of the file's extension, is treated as a php file. We'll put these include files in a subdirectory called include, and take the. inc suffix file. Below we will write the common content of these sites into the file.





Total Station general variable setting: Common.inc


;?


//Total Station general variable


$MyEmail = "phptalk@tnc.org";


$MyEmailLink = "<a href= ' mailto: $MyEmail ' > $MyEmail </a>";


$MyName = "PHP Talk";


$MySiteName = $MyName. "' s home Page ";


?>





General page head: Header.inc


;?


//Define General page head


?>


<HTML>


<HEAD>


<TITLE>


;? echo "$MySiteName-$title";?>


</TITLE>


</HEAD>


<BODY>


<H1>


;? echo "$MySiteName";?>


</H1>


<H2>


;? echo "$title";?>


</H2>


<HR>





Common page bottom: Footer.inc


;?


//Common 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 for the time being.


</P>


<P>


But I hope I can get up 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 through 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 just need to change the corresponding file. If you want to change your e-mail address or even your name, just modify the Common.inc file. It's also worth noting that you can include files with any file name or file extension in your file, and you can even include files from other sites.








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.