You are here:Home » PHP » Difference Between Include Once and Require Once in PHP

Difference Between Include Once and Require Once in PHP

One of the most common questions I get asked by PHP beginners is, Why is 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.

PHP include Function

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.
include 'main_page.php';
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 it's own file and use the include function to add the file to the page.

include 'header.php';
<div id="content">
</div>
include 'footer.php';
If the file that you want to include is not found then this function to return a PHP warning, which is different to the require function which will return a fatal error.
The file path given to the function can be either absolute starting with a / or relative by placing a .. before the file.

PHP include_once Function

The include_once function is exactly the same as the include function except it will limit the file to be used once.
include_once 'main_page.php';
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 a 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 a include_once.

PHP require Function

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 continuing, where 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 'main_page.php';

PHP require_once Function

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.
require_once 'header.php';
<div id="content">
</div>
require_once 'footer.php';

1 comment: