PHP | (Include and Require)
As we know PHP allows us to create various functions and various elements that are used multiple times in multiple pages. Scripting the same function in multiple pages is a task of great effort and would consume time. This can be avoided if we follow and use the concept of file inclusion which helps us to include various files including text or codes into a single program which saves the effort of writing the full function or code multiple times. This also provides another advantage. If we want to change any code then instead of editing it in all the files, we just need to edit the source file and all codes will be automatically changed. There are two functions which help us to include files:
- include() function
- require() function
The include() function
This function is used to copy all the contents of a file called within the function, text wise into a file from which it is called. This happens before the server exceutes the code. Example:
Lets have a file called even.php with the following code:
<?php // file to be included echo "Hello GeeksforGeeks"?> |
Now let us try to include this file into another php file index.php file. We will see that the contents of both the file are shown.
<?php include("even.php"); echo "<br>Above File is Included"?> |
Output:
![]()
The require() function
The require() function performs same as the include() function. It also takes the file that is required and copies the whole code into the file from where the require() function is called. There is a single difference between the include() and require() function which we will see following this example:
Lets have a file called even.php with the following code:
<?php // file to be included echo "Hello GeeksforGeeks"?> |
Now if we try to include this file using require() function this file into a web page we need to use a index.php file. We will see that the contents of both the file are shown.
<?php require("even.php"); echo "<br>Above File is Required"?> |
Output:
![]()
include() VS require()
The both function acts as same and produce same results, but if by any chance a fatal error is arised, then the difference comes into the surface. Let’s look at the following code:
<?php include("even.php"); echo "<br>Above File is Included"?> |
Now if we don’t have a file named even.php, then in the case of the include(), following output will be shown with warnings about missing file, but atleast the output will be shown from the index.php file:

In the case of the require(), if the file PHP file is missing, a fatal error will rise and no output is shown and the execution halts.

This is the only difference. This also shows that require() function is better than include() function since the script should not continue executing if files are missing or such error is generated.
Recommended Posts:
- Difference between require-dev and require in PHP?
- How to include one CSS file in another?
- SASS | @mixin and @include
- AngularJS | ng-include Directive
- How to include() all PHP files from a directory ?
- What is the best way to include CSS file? Why use @import?
- Include Bootstrap in AngularJS using ng-bootstrap
- How to include a JavaScript file in another JavaScript file ?
- How to hide or show one record from an ng-repeat within a table based on ng-click?
- How to check whether an array is subset of another array using JavaScript ?
- How to set placeholder value for input type date in HTML 5 ?
- How to check if a number evaluates to Infinity using JavaScript ?
- How to print a number with commas as thousands separators in JavaScript?
- HTML | Marquee scrolldelay attribute
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



