One of the most common questions I get asked by PHP beginners is, Why are there 4 ways to include a file on your page?

There is include(), include_once(), require() and require_once().

What do these do? What’s the difference between them?

In this article, we’re going to look at all these different functions and will talk about an example of when you need to use each of them.

include()

The include function is used in PHP when you want to include a file within the current process. It takes one argument which will be a string to the file path you want to include.

The code inside the included file will then run when the include function is called.

This can be used in a PHP templating system where you have a header at the top of the page and the header is going to be the same on all pages. You will put this code inside its own file and use the include function to add the file to the page.

<?php include "header.php"; ?>
<div id="content">
<p>Content goes here</p>
</div> <!-- #content -->
<?php include "footer"; ?>

include_once()

The include_once function is exactly the same as the include function except it will limit the file to be used once.

The include function will allow you to include the same file multiple times so you can use it within a loop.

foreach($products as $product) {
    // will display all products.php
    include 'product.php';
}

This will include the product.php file as many times as it loops through the $products array, but if this was using an include_once function it will only display the product.php file once.

foreach($products as $product) {
    // will only display one product
    include_once 'product.php';
}

A more practical use of this function is if you define any functions in the included file to avoid redefinition of a function you should include it with an include_once.

require()

The require function acts just like the include function except if the file can not be found it will throw a PHP error. As the name suggests this file is required for the application to work correctly.

This will be a fatal error E_COMPILE_ERROR which will stop the application from continuing, whereas the include function will just return a warning but will continue with the application.

The require function is used exactly the same as the include function.

require 'core.php';

require_once()

The last of the four functions is the require_once, which is a combination of the require and include_once function. It will make sure that the file exists before adding it to the page if it’s not there it will throw a fatal error. Plus it will make sure that the file can only be used once on the page.

This function is the most strict out of the four functions and is the function I use most when constructing the page. Going back to the example used in the include function the require_once function is what you should use when displaying things like the website header and footer. This is because you always want these files to be here if they are not here you want the site to error, and you only want these to appear once on the page.

<?php require "header.php"; ?>
<div id="content">
<p>Content goes here</p>
</div> <!-- #content -->
<?php require "footer"; ?>

Reference

The Disqus comment system is loading ...
If the message does not appear, please check your Disqus configuration.