PHP File Inclusion

File Inclusion using require() and include()

You can include the content of one PHP file into another PHP file before the server executes it. PHP has include() and require() statement takes all the code or content from the specified file and copies it into another file.

What is the benefit of File Inclusion?

File Inclusion concept is very useful when you want to include the same code on multiple pages in a website. You can simply put that common code in a separate file and include that file into multiple pages where you want that file’s code. The real benefit is when you need to change the code of that common file, you need to change only at one place.

There are two PHP functions which can be used to included one PHP file into another PHP file.

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

This is a strong point of PHP which helps in creating functions, headers, footers, or elements that can be reused on multiple pages. This will help developers to make it easy to change the layout of complete website with minimal effort. If there is any change required then instead of changing thousand of files just change included file.


PHP 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 problem in loading a file then the include() function generates a warning but the script will continue execution.

<?php
echo "<p>Learn Online Free Courses – Web Tutorials – Articles – Interview Questions – Tips for IT Professionals
 . " tutorialsclass.com</p>";
?>
<html>
<body>
 
<?php include 'header.php';?>
<h1>Welcome to our website tutorialsclass.com!</h1>
<p>Some text.</p>
<p>Some more text.</p>
 
</body>
</html>

PHP require() Function

The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.

So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.

<?php
echo "<p>Learn Online Free Courses – Web Tutorials – Articles – Interview Questions – Tips for IT Professionals
 . " tutorialsclass.com</p>";
?>
<html>
<body>

<?php include 'header1.php';?>
<h1>Welcome to our website tutorialsclass.com!</h1>
<p>Some text.</p>
<p>Some more text.</p>

</body>
</html>

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:


Common Mistakes in PHP File Inclusion

If you find error while including PHP file, check following things:

  1. While using the full path, do not forget to include ‘http://’ in URL. For example: 
    • <?php include ‘http://localhost/includes/header.php’; ?>
  2. While including a file from the same folder, just use filename: For example:
    • <?php include ‘header.php’; ?>
  3. Make sure your file exists at a given path with proper file name and extension.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.