PHP Include and Require

File Inclusion

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.
Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

You can include the content of a PHP file into another PHP file before the server executes it. 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.


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 "Learn Online Free Courses – Web Tutorials – Articles – Interview Questions – Tips for IT Professionals . " shishirkant.com";
?>

<html>
<body>
<?php include 'header.php';?>
<h1>Welcome to our website shishirkant.com!
<p>Some text.
<p>Some more text.
</body>
</html>

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
 . " shishirkant.com</p>";
?>
<html>
<body>

<?php include 'header1.php';?>
<h1>Welcome to our website shishirkant.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 full path, do not forget to include http// in url. Form example: <?php include 'http://localhost/includes/header.php'; ?>
  2. While including file from the same folder, just use file name: <?php include 'header.php'; ?>
  3. While including file from the sub-folder, just includes subfolders name (but not localhost or htdocs)
  4. Make sure your file exists at given path with proper file name and extension.
Follow Us On