| by Arround The Web | No comments

All About include() and require() Functions in PHP

In PHP, there are two methods for inserting the content from one PHP file into the other file. One is the include() function and the other is require() function. Both functions are similar but there is only one difference they handle errors differently. The include() function generates a warning if an error occurs, but the program will still run. The require() generates a fatal error and stops the execution of the PHP script.

In this article, we will discuss the include() and require() functions with their differences in PHP.

include() Function in PHP

The include() function in PHP is used to put the data of one file into the other PHP file. If the error occurs the next part of the script will continue to run and the execution will not stop.

The syntax for using the include() in PHP is:

include(file-name);

Let’s take an example to know how the include() function works in PHP. I have created the PHP file named a test.php and included the following code:

<html>  
<body>  
<a href="https://linuxhint.com/">Home</a>
<a href="https://www.youtube.com/c/linuxhint/"> Videos </a>
</body>  
</html>

Next, I created another PHP file with the name linuxhint.php file and used the include() function to add the content of the test.php file in this file. The following is the code snippet:

<html>
<body bgcolor="White">  
<?php include("test.php"); ?>  
<h2><b>Welcome to Linux Hint </b></h2>  
<p>All content on this website is for educational purposes only</p>
</body>  
</html>

Run the file on any browser. Make sure the XAMPP is installed:

require() Function in PHP

It is used to put the content of one file into the other in PHP, if there is any error occurs the execution of the script will stop. The below mentioned is the general syntax for using the require() function in PHP:

require(file-name);

I have used the same files to illustrate the working of the require() function in PHP:

<html>  
<body bgcolor="White">  
<?php require("test.php"); ?>  
<h2><b>Welcome to Linux Hint </b></h2>  
<p>All content on this website is for educational purposes only</p>
</body>  
</html>

The output of the above code is:

Include() vs require() in PHP

Both functions almost perform similar actions except in case of error. If the file is missing the include() function displays the error and continues the execution of the remaining script. For example, if the test.php file is not present in the same directory or missing, the include() function gives a warning about the missing file but displays the output.

On the other side, there is no file named test.php, executing the code will display the following error:

The following are the key differences between the include() and require() functions in PHP:

include() require()
If the file is missing, the include function does not stop running the PHP script. The require() function gives the warning and stops the execution of the PHP code if the file is misnamed or not found
It only gives an error warning (E-WARNING) and does not produce a fatal error. It produces a fatal error (E-COMPILE-ERROR).
It can be used in loops and control structures. It cannot be used in loops and control structures.
The file can return a value. The file cannot return a value.

Bottom Line

The include() and require() functions are helpful when you want to utilize the same PHP code in multiple files. They are mostly the same except for how they handle errors. The include() function only displays a warning and continues and the require() function stops the execution and gives only an error.

Share Button

Source: linuxhint.com

Leave a Reply