Home arrow PHP Tutorials

Translate This Website
Saturday, 22 November 2008
Web Development
Request a Quote
Script Categories
RSS Media Grabber Funny Videos & Pictures Life Stories (Flash) Video Search & Download Video Downloader Script Country on Sale Advanced Polls Phone Upload/SMS Scripts PhotoCube Script SlideShow Creation Script Create Custom Smiley Script Short URLs & Subdomains Put your text on images Watermark & Image Hosting Joomla Components Low Cost Scripts
Partners
WEBSITE TRAFFIC
Credit Cards
Online HTML Editor
Wordpress Themes
Custom Programming
Scripts and Stuffs
Hot php scripts
Add your scripts
Script Search
php scripts directory
script heaven
the cgi site
Scripts
Software Development
Free Icons
SEO Services


PHP - The Include Function Print E-mail

The include function takes a file name and simply inserts that file's contents into the script that calls used the include function. This means that you can type up a common header or menu file that you want all your web pages to include. When you add a new page to your site, instead of having to update the links on several web pages, you can simply change the Menu file.

 Example

menu.php Code:

<html>
<body>
<a xhref="http://www.example.com/index.php">Home</a> -
<a xhref="http://www.example.com/about.php">About Us</a> -
<a xhref="http://www.example.com/links.php">Links</a> -
<a xhref="http://www.example.com/contact.php">Contact Us</a> <br />

 

Save the above file as "menu.php". Now create a new file, "index.php" in the same directory as "menu.php". Here we will take advantage of the include function to add our common menu.

index.php Code:

<?php include("menu.php"); ?>
<p>This is my home page that uses a common menu to save me time when I add
new pages to my website!</p>
</body>
</html>

Display:

Home - About Us - Links - Contact Us

This is my home page that uses a common menu to save me time when I add new pages to my website!

And we would do the same thing for "about.php", "links.php", and "contact.php". Just think how terrible it would be if you had 15 or more pages with a common menu and you decided to add another web page to that site. You would have to go in an manually edit every single file to add this new page, but with include files you simply have to change "menu.php" and all your problems are solved. Avoid such troublesome occasions with a simple include file.

What do Visitors See?

If we were to use the include function to include a common menu on each of our web pages, what would the visitor see if they viewed the source of "index.php"? Well, because the include function is pretty much the same as copying and pasting, the visitors would see:

View Source of index.php to a Visitor:

<html>
<body>
<a xhref="index.php">Home</a> -
<a xhref="about.php">About Us</a> -
<a xhref="links.php">Links</a> -
<a xhref="contact.php">Contact Us</a> <br />
<p>This is my home page that uses a common menu to save me time when I add
new pages to my website!</p>
</body>
</html>

The visitor would actually see all the HTML code as one long line of HTML code, because we have not inserted any new line characters. We did some formatting above to make it easier to read. We will be discussing new line characters later.

Include Recap

The include command simply takes all the text that exists in the specified file and copies it into the file that uses the include function. Include is quite useful when you want to include the same PHP, HTML, or text segment on multiple pages of a website. The include function is used widely by PHP web developers.

The next lesson will talk about a slight variation of the include function: the require function. It is often best to use the require function instead of the include function in your PHP Code.

 Credits: www.tizag.com





Reddit!Del.icio.us!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!Free social bookmarking plugins and extensions for Joomla! websites!
 
< Prev   Next >
Services
Script Installation
Hosting
Free Image Hosting
PageRank Checker
Login Form
On Sale
Watermark and Host Images
Watermark and Host Images
$45.00
$35.00
You Save: $10.00
Add to Cart
Home Announcements Support Newsletter Forum PHP Articles PHP Tutorials Scripts FAQs Free Scripts Links Contacts
Copyrighted © 2008 phppod.com