Page Stats
Visitor: 404
Include one PHP file into another
Sometime it is necessary to include contents of one file into another file. For example: if you have 5 pages in your website and all pages have some common contents like header, footer etc. So, you can create a page which have header contents, another page with footer contents and include that header and footer page/file in all or some pages of your website. This process can be done with the help of PHP include and require statement.
PHP include statement advantages:
- Saves time instead of coping the code in all pages, we have to write only one line.
- Reduce efforts if there is changes in the code, instead of make changes in all web pages, we have to change only in one page and changes will reflect in all the pages.
PHP include statement
PHP include is very useful statement. It can be used to include the common text, common HTML code, or common PHP code on multiple web pages.
PHP include statement syntax:
<?php include ('filename.php');?> <?php include 'filename.php';?> <?php include "filename.php";?> <?php include 'foldername/filename.php';?>
PHP include statement can be used with or without parentheses, single quote and double quotes both are valid.
Example 1: In this example, we will create a header, menu, and footer file, and include in a main file.
header.php<?php echo "<h1> Website Heading </h1>"; <hr /> ?>menu.php
<?php echo '<a href="home.php">Home</a> - <a href="html.php">HTML Tutorial</a> - <a href="css.php">CSS Tutorial</a> - <a href="js.php">JavaScript Tutorial</a> - <a href="php.php">PHP Tutorial</a>'; ?>footer.php
<div> <p>© copyright 2020, All right reserved.</p>"; </div>
Include header.php, menu.php, footer.php file:
<html> <body> <?php include 'header.php';?> <div class="menu"> <?php include 'menu.php';?> </div> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> <?php include 'footer.php';?> </body> </html>
PHP require statement
The require statement is also used to include a file same as include statement. Example to use require statement is as follow:
<html> <body> <h1>Welcome to my home page! </h1> <?php require 'filename.php'; ?> </body> </html>
You can also include files of other extension like html, txt etc.
PHP include vs require
Both, include and require statement is use to include a file, but there is one big difference. When a file is included with the include statement and PHP cannot find it, the script will produce a warning message but continue to execute rest of the code, whereas in require statement if the file is not found it will produde a error message and also stop the execution. So, if your code is depend on the included file than you require statement, if your code is not depend on the included file you can use include statement.
PHP include_once and require_once statement
If you include the same file(function, class) more than once your code may cause conflict and may give error message. To prevent this situation, PHP provides include_once and require_once statement. PHP include_once and require_once syntax:
<?php include_once ('filename.php');?> <?php require_once ('filename.php');?>