Skip to content
Related Articles

Related Articles

PHP | (Include and Require)
  • Last Updated : 09 Mar, 2018

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :