In this article, we will see what include() & the require() functions is, also will know how these functions affect the execution of the code, their differences & usage in PHP, along with understanding their implementation through the examples. 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 lots of time & also impact the execution of the code. 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 that help us to include files:
We will understand both the function & their usage through the examples.
PHP 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 executes the code.
Example: This example is using the include() function in PHP.
even.php
<?php // File to be included echo "Hello neveropen" ; ?> |
Now, try to include this file into another PHP file index.php file, will see the contents of both the file are shown.
index.php
<?php include ( "even.php" ); echo "<br>Above File is Included" ?> |
Output:
PHP 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.
Example: This example is using the require() function in PHP.
even.php
<?php // File to be included echo "Hello neveropen" ; ?> |
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 files are shown.
index.php
<?php require ( "even.php" ); echo "<br>Above File is Required" ?> |
Output:
Difference between include() function vs require() function: Both functions act as same and produce the same results, but if by any chance a fatal error arises, then the difference comes to the surface, which we will see following this example. Consider the following code:
index.php
<?php include ( "even.php" ); echo "<br>Above File is Included" ?> |
Output: Now, if we don’t have a file named even.php, then in the case of the include(), the following output will be shown with warnings about a missing file, but at least 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 the include() function since the script should not continue executing if files are missing or such an error is generated.