Press "Enter" to skip to content

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

0

Difference between include() and require() functions?

This Both function 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';

or

include('filename');

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

<a href="http://www.techlifediary.com">Home</a>
<a href="http://www.techlifediary.com/p/about_20.html">About</a>

<a href="http://www.techlifediary.com/p/privacy-policy.html">Privacy</a>

<a href="http://www.techlifediary.com/category/php/">Programming</a>

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

<html>
<title>HomePage</title>
<body>
<?php include('header.php'); ?>
</body>
</html>

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

includeDemo
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 a example without using a file we are going to use require() function.

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

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

Use require when the file is required by the application.

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

How can we get the IP address of the client in PHP?
How to make a login page in PHP

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.