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:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// file to be included
echo "Hello GeeksforGeeks"
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
    include("even.php");
    echo "<br>Above File is Included"
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// file to be included
echo "Hello GeeksforGeeks"
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
    require("even.php");
    echo "<br>Above File is Required"
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
    include("even.php");
    echo "<br>Above File is Included"
?>

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.