PhpMyanmar.com logo Php Tutorial
Home
Php Tutorial
 Echo
 If

Include File
Show Myanmar Version
 show-myanmar-font
Printer Friendly Page

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

include("xxx.php")

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 "menu.inc".

menu.inc 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 "menu.inc". Now create a new file, "index.php" in the same directory as "menu.inc". Here we will take advantage of the include function to add our common menu.

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

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

Processing time -- 0.0176 seconds.

c81ab10b2dde7566bc34d174448e7d55