Difference between PHP include and require statements

By Faruque Ahamed Mollick

The include and require statements in PHP used to take all the file content in a specific file and copy it in the file that uses the PHP include and requires statements. These functions can take text code or markup and include it in that file.

The main purpose of using PHP include and require statement is to reduce the time by increasing code reusability. You write some code once and it can be used in several other files.

PHP Weather Forecast Script That Uses OpenWeatherMap API

Below is the usage of PHP include and require statements:

include 'filename';

 

require 'filename';

Well, you have to use the complete filename with extension:

include 'header.php';

And for PHP require:

require 'header.php';

PHP include and require are not functions and that’s, why it does not necessary to uses brackets. They are actually constructed. It simply can contain the file path inside ‘ ‘ like include ”file.php”  or require “file.php.

So what is the difference between PHP include and require?

Many PHP beginners have a question and they want to know the difference between PHP include and require statements. So here I am going to tell you the differences between include and require statements of PHP.

Replace Multiple Items In String Using PHP str_replace Function

How to get HTML form data in PHP variable?

PHP include

PHP include statement used to include the content of a file in several other files. The syntax of this statement is include ”filename”. If the included file does not exist, it simply shows a warning message. But all the rest of the code will run without any problem.

Suppose you have included a file which does not exist in the directory like below:

<?php
include "file.php";
echo "Code after PHP include the statement";
?>

Now file.php does not exist. In this case, you will see the warning message, but you will able to see the text “Code after PHP include the statement”. That means all the other code after this include statement with a file that does not exist will still execute.

PHP require

PHP require statement also used to copy the content of a file in other files just like include statement do. But there is a difference. In the case of PHP include statement, you see that the rest of the codes can execute if the file does not exist. But PHP require statement, it will return you a fatal error and the code after the require statement does not execute.

See the below example code:

<?php
require "file.php";
echo "Code after PHP require statement";
?>

In the above code, if the file.php file does not exist in the directory, it will give you a fatal error. You will not get the return value from PHP echo that we have used after PHP require statement. In the case of PHP include, you see that it returned “Code after PHP require statement”, but here in the case of PHP require statement the ‘file.php” must require and it is returning the fatal error stopping all the other code from being executed after this statement.


I hope you have understood the main difference between PHP include and require statement from this tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *