The include() function takes all the content in a specified file and includes it in the current file.
If an error occurs, the include() function generates a warning, but the script will continue execution.
Example 1
Assume that you have a standard header file, called "header.php". To include the header file in a page, use the include() function:
< html >
< body >
< ?php include("header.php"); ? >
< h1 >Welcome to my home page!< /h1 >
< p >Some text.< /p >
< /body >
< /html >
Example 2
Assume we have a standard menu file, called "menu.php", that should be used on all pages:
< a href="/default.php">Home< /a >
< a href="/tutorials.php">Tutorials< /a >
< a href="/references.php">References< /a >
< a href="/examples.php">Examples< /a >
< a href="/about.php">About Us< /a >
< a href="/contact.php">Contact Us< /a >
All pages in the Web site should include this menu file. Here is how it can be done:
< html >
< body >
< div class="leftmenu" >
< ?php include("menu.php"); ? >
< /div >
< h1 >Welcome to my home page.< /h1 >
< p >Some text.< /p >
< /body >
< /html >
If you look at the source code of the page above (in a browser), it will look like this:
< html >
< body >
< div class="leftmenu" >
< a href="/default.php">Home< /a >
< a href="/tutorials.php">Tutorials< /a >
< a href="/references.php">References< /a >
< a href="/examples.php">Examples< /a >
< a href="/about.php">About Us< /a >
< a href="/contact.php">Contact Us< /a >
< /div >
< h1 >Welcome to my home page!< /h1 >
< p >Some text.< /p >
< /body >
< /html >