Friday, September 25, 2020

A Definitive Guide to PHP File Inclusion



Hello, dear readers! welcome back to another section of my tutorial on PHP. In this section of our PHP tutorial, we will be studying about the various ways in which files can be included into another PHP file.

You can include the content of a PHP file into some other PHP file before the web server executes it. There are two functions which can be used to include a PHP file into another PHP file.

  • The include() Function
  • The require() Function

This is a vital point in PHP which helps in the creation of functions, headers, footers or elements that can be reused on multiple pages. This makes it easy for developers to change the layout of the entire site with a very little effort. If there is any change required, instead of changing thousands of files just change the included file.



The include() Function

The include() function takes all the text in a specified file and copies it into the file that makes use of the include() function. If there is any problem in loading the file, then the include() function generates a warning message but the script will continue with the execution.

Example

Assuming you want to create a simple nav menu for your website, then create a file menu.php with the following content below -

<a href="http://www.webdesigntutorialz.com/index.htm">Home</a> - 
<a href="http://www.webdesigntutorialz.com/ebxml">ebXML</a> - 
<a href="http://www.webdesigntutorialz.com/ajax">AJAX</a> - 
<a href="http://www.webdesigntutorialz.com/perl">PERL</a> <br />

Now create as many pages as you want and include this file to create a header in your site. For example your text.php file can then have the following content -

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

Output

When the above code is executed, it will produce the following result -



The require() Function

This require() function takes all the text in a specified file and copies it into the file that makes use of the include() function. If there is any issue in loading the file then the require() function generates a fetal error and halts the execution of the script.

There is no difference in include() and require() function, except how they both handle error conditions. Note - it is highly recommended to make use of the require() function rather than the include() function, and that is because scripts should not continue with its execution if files are missing or misnamed.

Example

Now you can try above example using the require() function and it will produce same result. But if you try the two examples where the file does not exist, then you will get a different result -

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

Output
When the above code is executed, it will produce the following result -

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

Now let's try the same example with the require() function -

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

This time around, file execution halts and nothing is displayed to the screen.

Note - You may end up getting plain warning messages or fatal error messages or no message at all. This is dependent on your PHP Server Configuration.

RECOMMENDED: PHP Global Variables

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial post, we will be discussing about the PHP Files and I/O.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.