PHP Tutorial: File Inclusion

File Inclusion

We can insert the content of an external PHP file into a PHP file before the execution of php script. There are basically two PHP functions which are used to include a PHP file into another PHP file.

  • include() Function
  • require() Function

These functions help us to perform some common tasks throughout the whole website. Like we can create common headers, footers, functions or elements that can be reused on multiple pages. This will help us to make easy to change the layout of website will less efforts.

The include() Function:
The include() function takes all the content of a file and copied it into the file that used the include function. If the included file is not found or there is any error then it generates a warning without breaking the execution of php script.
Suppose we want to create a common header for our website. Then we can create a file header.php like below content.

<header>
<img src=”logo.png” alt=”” />
<a herf=” https://quizsolution.in/”>Home</a>
<a herf=”https://quizsolution.in/categories.php?get=tutorials”>Tutorial</a>
</header>

Now we can include this header.php on all the pages of website where we require these elements. For example let us create a new file test.php in which we are including the header.php

<html>
   <body>
   
      <?php include("menu.php"); ?>
      <p>This is an example to show how to include PHP file!</p>
      
   </body>
</html>

 

The require() Function:
The require() function copied all the external file content into the file that uses the require() function. If the file not found or it generates any error then it produces fatal error and stop the php script execution.
So there is only the difference between the require() and include() is that include() produce the warning and the execution continue but in require() it produce the fatal error and stop the execution. Otherwise both done the same task.
Also we can use the above script with the require() function and it will give us the same result. 
 

<html>
   <body>
   
      <?php include("headerxxx.php"); ?>
      <p>This is an example to show how to include wrong PHP file!</p>
      
   </body>
</html>

Suppose the included file "headerxxx.php" not found then it will giv the following result.

This is an example to show how to include wrong PHP file!

Now lets try same example with require() function.

<html>
   <body>
       
       <?php require("headerxxx.php"); ?>
       <p>This is an example to show how to include wrong PHP file!</p>
   
   </body>
</html>

This time file execution stops and nothing is displayed.

NOTE − You may get plain warning messages or fatal error messages or nothing at all. This depends on your PHP Server configuration.

 

PHP include vs. require

The require statement is also used to include a file into the PHP code.
However, there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute:
If we do the same example using the require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:
 

PHP require_once()
require_once() statement can be used to include a php file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions.
If a.php is a php script calling b.php with require_once() statement, and does not find b.php, a.php stops executes causing a fatal error
Syntax:

require_once('path of the external php file');

PHP include_once()
The include_once() statement can be used to include a php file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions.
If a.php is a php script calling b.php with include_once() statement, and does not find b.php, a.php executes with a warning, excluding the part of the code written within b.php.

Syntax:

include_once('path of the external php file');

 

Next Topic