In this tutorial, we will see how to include one PHP file into another PHP file.


The PHP include() and PHP require() statement allow you to include the content of one PHP file into another PHP file. You can save your time and work using the PHP include() and PHP require() statements instead of typing the entire script multiple times.

With the help of PHP include() and PHP require() statement, we can utilize HTML code or PHP script code in many PHP script code. Let’s see the basic syntax of the PHP include() or PHP require() statement can be given listed below:

include("file path/filename"); Or include "file path/filename";
require("file path/filename"); Or require "file path/filename";


PHP include Examples

Example 1

Let’s suppose we have a standard menu php code file which is called “menu.php”:

<?php
echo '<a href="https://www.netzole.com/">Home</a> 
<a href="https://www.netzole.com/php-tutorial/">PHP Tutorial</a>
<a href="https://www.netzole.com/nodejs-tutorial/">Nodejs Tutorial
</a> 
<a href="https://www.netzole.com/python-tutorial/">Python Tutorial</a> 
?>

To include the menu file in a page, use the PHP include statement:

Example


<html>
<body>

<div class="menu">
<?php include 'menu.php';?>
</div>
</br>
<h1>Welcome to home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>

</body>
</html>

output:


Welcome to home page!

Some text.

Some more text.


Example 2

Let’s suppose we have a standard footer php code file which is called “footer.php”:

<?php
echo "<p>Copyright &copy; 2017-" . date("Y") . " Netzole.com</p>";
?>

To include the menu and footer file in a page, use the PHP include statement:

Example


<html>
<body>

<div class="menu">
<?php include 'menu.php';?>
</div>

<h1>Welcome to Netzole.com!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>

output:


Welcome to Netzole.com!

Some text.

Some more text.

Copyright © 2017-2019 Netzole.com




PHP require

PHP require is used to similar to PHP include. Let’s see a PHP require example.

Example:

File: require.php


<html>
<body>

<div class="menu">
<?php require 'menu.php';?>
</div>
</br>
<h1>Welcome to home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>

</body>
</html>

output:


Welcome to home page!

Some text.

Some more text.




PHP include vs PHP require

  • require will generate a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only generate a warning (E_WARNING) and the script will continue

At the top of, there is one big difference between PHP include and PHP require; when a file is included through the PHP include statement and PHP file cannot find it, the script will continue to execute: Let’s assume menu file(menu.php) does not exist.

Example


<html>
<body>

<div class="menu">
<?php include 'menu.php';?>
</div>
</br>
<h1>Welcome to Netzole.com!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>

output:

Welcome to Netzole.com!

Some text.

Some more text.

Copyright © 2017-2019 Netzole.com



On the other hand,If we given the same example using the PHP require statement, the echo statement will not be execute the script because the script execution dies after the PHP require statement returned a fatal error:

Example


<html>
<body>

<div class="menu">
<h1> Home page Title!</h1>
<?php require 'menu.php';?>
</div>

<h2>Welcome to Netzole.com!</h2>
<p>Some text.</p>
<p>Some more text.</p>
<?php require 'footer.php';?>
</body>
</html>

output:

Home page Title!


Pin It on Pinterest

Shares
Share This