Include() and Require() Function in PHP
Php 07-Apr-2022

Include() and Require() Function in PHP

Require and Include function in PHP; In this tutorial, we will explain to you the require and include the function of PHP with these function definitions, syntax, parameters, difference and example.

In this tutorial, also we will demonstrate the difference between include and require in PHP with example.

Before we start the demonstration of PHP include() and require() function, first of all, we need to know about the difference between PHP include() and require() function. You can see the difference below:

Include() and Require() Function in PHP

In PHP, Require and Include function both are the built-in function of PHP. Both functions performs the same action.

Include PHP Function

Definition:- The include() function is built-in PHP function, which is used to include a file into the PHP script.

Note:- The PHP include() function, it will produce a warning error, without halt the execution of PHP script.

Syntax:-

The basic syntax of include function is the following:

include 'path/filename.php';

Example of include() function

The example of PHP include() function is the following:

<html>
<head>
//include file using include() function
<?php include 'path/head.php';?>
 
</head>
<body>
 
<h1>include() function Example</h1>
 
</body>
</html>

Require PHP Function

Definition:- The require() function is built-in PHP function, which is used to include a file into the PHP script. :-

Note:- The PHP require() function, it will produce a fatal error and halt the execution of php script.

Syntax:-

The basic syntax of the require function is the following:

require 'path/filename.php';

Example of Require() function

The example of PHP require() function is the following:

<html>
<head>
//require file using require() function
<?php require 'path/head.php';?>
 
</head>
<body>
 
<h1>include() function Example</h1>
 
</body>
</html>

Question:- What is difference between include and require in PHP?

Answer:- In PHP, Require and Include function both are the built-in function of PHP. Both functions perform the same action. The main difference is that the include() function produces a warning, without halt execution of PHP script, while the require() function produces a warning and a fatal error i.e. the script will halt the execution.