logo Php Tutorial
Php Tutorial

Include File
Show Myanmar Version
Printer Friendly Page

An external file can be included anywhere in a Php file by using the Php include function:-


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.

When a file is included:-

  • The included file inherits the functions, classes and variables above the line on which the include occurs.
  • From that point forward, all functions, classes and variables defined in the included file have the global scope.
An Include Example

Say we wanted to create a common menu file that all our pages will use. A common practice for naming files that are to be included is to use the ".inc" extension. Since we want to create a common menu let's save it as "". Code:
<html> <body>
<a href="index.php">Home</a> -
<a href="about.php">About Us</a> -
<a href="links.php">Links</a> -
<a href="contact.php">Contact Us</a> <br />

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

index.php Code:
<?php include(""); ?>
         Web pages that uses a common menu just need to include
        '' and all will have uniform menu.(save a lot of coding
        works too)
Home - About Us - Links - Contact Us

Web pages that uses a common menu just need to include '' and all will have uniform menu.(save a lot of coding works too)

Processing time -- 0.0134 seconds.