We now have a youtube channel. Subscribe!

A Definitive Guide to PHP File Inclusion

PHP File Inclusion


Hello folks! welcome back to a new section of our 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 server executes it. There are two PHP functions which can be used to include one 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 website with a little effort. If there is any change needed, instead of changing thousands of files just change the included file.


PHP REQUIRE() and INCLUDE() Function

The include() Function

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

Example

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

<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 like and then include this file to create a header. For example now your text.php file can 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

The 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 problem in loading a file, the require() function generates a fetal error and halt the execution of the script.

So there is no dissimilarity in include() and require() function, except how they handle error conditions. It is recommended to use the require() function rather than include(), because scripts shouldn't 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 will 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 us 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 get plain warning message or fatal error messages or no messages at all. This is dependent on your PHP's Server Configuration.


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

Feel free to ask your questions where necessary and we 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.

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.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain