PHP Include and Require

In this PHP tutorial, you will learn all about the different file insertions methods include and require. We will learn in detail about the uses of include and require methods and how to use them and their differences.

What is include and require in PHP?

PHP allows us to develop a variety of widgets and methods that are reused across several pages. Scripting these routines on numerous pages takes a long time. As a result, use the technique of file inclusion, which allows you to include files in numerous applications while saving you the work of creating the same code many times. "PHP allows you to include files, allowing you to reuse page content several times. When you wish to apply the same HTML or PHP code to several pages of a website, including files is quite useful." In PHP, there are two ways to include a file.

  1.   include() Function
  2.   require() Function

This helps in creating the functions, headers, footers, and components that can be reused across several pages. This will assist developers in making it simple to modify the style of a whole website with no effort. If a modification is necessary, instead of altering more files, just modify the included file.

What is include() function in PHP?

The include() function imports all the text in a given file into the current file that uses the include function. If there is an error in loading a file, the include() method emits a warning, but the script continues to run the remaining script. PHP include is used to include a file based on a path. You can specify either a relative or absolute path to the file.

Syntax

include 'filename';

Include function example: demo_include.php page


<h1>It is the included page</h1>

Include function example: demo.php page

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Include</title>
</head>
<body>
    <?php include 'demo_include.php' ?>
</body>
</html>

Output:


PHP - Form

In the above example, we can see that the page executed was ‘demo.php’ and the text displayed in the page ‘demo_include.php’ and we can see that by including the page on another page we can access it.

Output: if the page is not found

Output:


PHP - Form

If the page is not found then it will show the warning and execute the remaining script.

What is require() function in PHP?

The require() method inserts all of the text from a given file into the file that uses the require function. If there is an error in loading a file, the require() method then throws a fatal error and terminates the script's execution. There is no difference between require() and include() other than how they handle error scenarios. Because scripts should not continue to execute if files are missing or misnamed, it is preferable to use the require() method instead of include(). You may use the preceding example with the require() method to get the same effect.

Syntax


require 'filename';

Example: demo_require.php page


<h1>It is the included page</h1>

Example: demo.php page


<!DOCTYPE html>
<html lang="en">
<head>
    <title>Include</title>
</head>
<body>
    <?php require 'demo_require.php' ?>
</body>
</html>

Output:


PHP - Form

In the above example, we can see that the page executed was ‘demo.php’ and the text displayed in the page ‘demo_require.php’ and we can see that by including the page on another page we can access it.

Output:


PHP - Form

If the page is not found then it will throw an error and it won’t execute the remaining script.

What is the difference between the include and require method in PHP?

Both include and require are the same concept. However, if the file is missing or inclusion fails, the include method will generate a warning i.e., E_WARNING, and continue the execution of the remaining script and in case of require method, it will generate a fatal error, i.e., E_COMPILE_ERROR, and stop the execution of the remaining script.