File inclusion means using file inclusion statements to insert the code, text, and markup of one file into another file. PHP provides us with two statements to include files. They are the include() and the require() statements.
Including files becomes necessary because sometimes the same code needs to be executed on multiple PHP pages. Instead of writing the same code in multiple files the code can be written in one file and then the file can be included wherever necessary. Hence file inclusion is essentially a way to reuse code.
Eg: The header and footer of every PHP page in a website will generally be the same. Instead of writing code for the header and footer in every PHP file, we can encapsulate the code in a header and footer file. The header and footer file can then be included in every PHP page.
File inclusion provides us with the following advantages
- It enables us to reuse code saving a lot of time and effort.
- If any change is required in the website then instead of changing many files a user can just change the included file.
- The layout of the complete website can be changed with very less effort.
File inclusion has to happen prior to execution of the PHP file by the server. There are two in-built PHP functions which are used for including files. They are
- include() – This will just produce a warning if it fails and the script will continue to execute.
- require() – This statement will produce a fatal error if the included file is not found and the script execution is aborted.
Hence the difference between the 2 file inclusion statements is because of the way in which errors are handled.
include() Function
The include() statement inserts the contents of a specified file into another file which has the include statement.
If the include statements fails to include the file due to any issues then the include statement generates a warning E_WARNING. The script however continues to execute after the warning is generated.
The include statement should be used only in cases where the file inclusion is not absolutely necessary and the program will execute correctly even though the file is not included.
Syntax:
The basic syntax of the include() statement is
Example :
We can create a file for the menu of a website and then include this file in multiple PHP pages which require a menu. The following example creates a file mymenu.php with the following content.
We can then include this file on the pages which require the menu. For example, a testpage.php file can have the following content.
include_once Function
The functionality is same as the include function but it will only include the file only once.
So if you mistakenly include the file more than once using the include function and if the include files only contain some common functions defined by you, PHP may issue an error saying the function is already defined. So for such files, it is better to use an include_once function instead of an include function.
Note: If you want to be doing an include for output or if you need to include a file each time it is called, one should use include function and not include_once.
require() Function
The require() statement also inserts code from a specified file into the file in which the require is present. If the statement fails to load the file specified for inclusion then it generates a fatal error and the file execution is aborted.
Syntax:
It is always recommended that we use the require() function instead of the include() function. This is because if files are missing continuation of script execution can pose security issues, issues with the output and it is not recommended.
Example:
If you try to execute the following two scripts if the included file does not exist then you get different results. Using the include statement the output will be as follows:
require_once Function
The functionality is same as the require function but it will do the require file once.