PHP - File Inclusion

Admin   PHP   141  2020-10-23 11:07:00

You can insert the contents of one PHP file into another PHP file before the server executes it. There are 2 functions in PHP that can be used to do this.

Function include()
Function require()

This is a strong point of PHP that helps in creating functions, headers, footers or elements that can be reused in multiple pages. This will make it easier for programmers to change the layout of the web. If any changes are required, instead of having to change thousands of files, just change the included file.

PHP include() function

The include() function takes all the text in the specified file and copies them into the file using the include function. If there is a problem loading the file, the include() function generates a warning, but the script continues to execute.

Let's say you want to create a general menu for Website. Then create a file called menu.php in htdocs with the following content:

<a href="https://blogdev.net/index.jsp">Home</a> -
<a href="https://blogdev.net/php">PHP</a> -
<a href="https://blogdev.net/java">JAVA</a> -
<a href="https://blogdev.net/html">HTML</a> <br />

Now create as many pages as you need and insert this file to create the header. For example, test.php might have the following content.

<html>
   <body>
   
      <?php include("menu.php"); ?>
      <p>The example illustrates how to include files in PHP!</p>
      
   </body>
</html>

Save the above program in a file called test.php in htdocs , then open a browser and type http://localhost/test.php will produce the following result:

Include file in PHP

The require() function in PHP

The require() function takes all the text in the specified file and copies them to the file using the require function. If anything goes wrong during file loading, the require() function generates a Fatal Error and prevents the script's execution.

So there is no difference between require() and include() other than that they handle error conditions. We recommend that you use the require() function instead of include(), because the script should not continue to execute if the files are lost or named.

You can use the above example with the require() function and it will produce the same result. But if you try the following two examples, and if the file doesn't exist, you will get different results.

<html>
   <body>
   
      <?php required("xxmenu.php"); ?>
      <p>The example illustrates how to requiredfiles in PHP!</p>
      
   </body>
</html>

Save the above program in a file called test.php in htdocs , then open a browser and type the address http://localhost/test.php will give the results (I do it on Google Chrome):

Include file in PHP

The executable file this time is paused and shows nothing.

Save the above program in a file called test.php in htdocs , then open a browser and type the address http:// ocalhost/test.php will give the results (I do it on Google Chrome):

Include file in PHP
Note - You can get warning message or error message or nothing at all. This depends on your PHP Server configuration.