Press "Enter" to skip to content

What’s the difference between the include() and require() functions?


Difference between include() and require() functions?

This both functions help to include the content of other PHP files into another PHP file. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

include() Function

While using the include() function it adds the content of the page or file into the file where this function is used.If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.

Syntax :

include 'filename';



Let us take an Example of include() function, here I’m creating a header.php file which contains the menu.

<a href="">Home</a>
<a href="">About</a>

<a href="">Privacy</a>

<a href="">Programming</a>

Now Create a index.php file to add the content.

<?php include('header.php'); ?>

After including the Page into index.php will like this Image.

require() function

Using 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.

Now take an example without using a file we are going to use require() function.

<?php require('invalidfile.php'); ?>//file not in the source

This will Happen when  require() function will be used if the file will not available at the source.

Use requires when the file is required by the application.

Use includes when the file is not required and the application should continue when the file is not found.

How can we get the IP address of the client in PHP?
How to create login page in PHP with MySQL database