PHP Require and Include Function Example

PHP Require and Include Function Example

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

Recommended PHP Tutorial:

  1. Autocomplete Search Box in PHP MySQL
  2. Compare Arrays PHP | PHP array_diff() Function
  3. Get, Write, Read, Load, JSON File from Url PHP
  4. Functions: Remove First Character From String PHP
  5. Remove Specific/Special Characters From String In PHP
  6. How to Replace First and Last Character From String PHP
  7. Reverse String in PHP
  8. Array Push, POP PHP | PHP Array Tutorial
  9. PHP Search Multidimensional Array By key, value and return key
  10. json_encode()- Convert Array To JSON | Object To JSON PHP
  11. PHP remove duplicates from multidimensional array
  12. PHP Remove Duplicate Elements or Values from Array PHP
  13. Get Highest Value in Multidimensional Array PHP
  14. PHP Get Min or Minimum Value in Array

Leave a Reply

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