Introduction to PHP include_once
The include_once function in PHP consists and performs operations of the particular file while executing the script. This is hence the same as the include statement of PHP where the only change will be that if the script from another file is already present then it will not include it once again and this function include_once returns only TRUE. Hence as the name itself tells, the file here will be included by it only once.
include_once is basically used in place where the single file can be enclosed and operated more than one time while executing the script so this will help prevent different problems like reassignments of variable values, redefining functions, etc.,. This function is also very similar to the require_once function.
include_once('file name with its full path');
The upper syntax is self-explanatory.
Examples of PHP include_once Function
Let us consider that in the code we have the requirement of 2 different files like file p and file q. Check out the below condition:
p: include m, include n
m: include n
n: echo “This is function a”
Here suppose we execute the function p then first it includes the function m and n. As we can see below that function m also calls n hence including it. By this, we can make out that n will be included two times which is rather not appropriate. Hence by using the include_once function, we can limit its calling to only one time.
Hence include_once will be applicable in cases where suppose there are 2 models and one of them requires to call a database connection model. Here including it, every time may cause duplication error. So using include_once we can avoid the same which is intended to include only once.
Examples of PHP include_once
Following are the examples as given below:
// First file displaying present date
echo "Finding the current date here \n";
echo "today is:".date("Y-m-d");
This is the first file that we save by some name “Main.php” which we call in our next piece of code.
We are using include_once function to include our Main.php file two times in the above code. But as we can see in the output we get the echo display data only once because the second instance called is ignored. This is because the include_once() function ignores all the inclusions which are the same as the first one after its execution.
Let us consider there are 3 files to be run as below:
The code of FIRST_FUNCTION.php looks like below:
// declaring a function
echo 'first function called';
// using include function to include the above function
echo 'second function called';
Now when this THIRD_FUNCTION.php is executed we get an error because SECOND_FUNCTION.php file already includes the FIRST_FUNCTION.php already. The error will state that the function func() has been already declared in SECOND_FUNCTION.php and we already included in THIRD_FUNCTION.php which means that we have already included FIRST_FUNCTION.php twice. So to make sure that we only use FIRST_FUNCTION.php only once, we should make use of the include_once() function so our THIRD_FUNCTION.php can be modified to the one like below:
// using include_once function
So now when we run this code we will not get any errors because PHP will include the FIRST_FUNCTION.php only once to avoid throwing an error. Hence in such cases it is necessary to use include_once() function instead of include() function in the PHP code.
PHP include function although very similar to the require function, we do find a few dissimilarities between them and they are as follows:
- Include function throws a warning (E_WARNING) whenever an error takes place whereas the require function doesn’t.
- The script execution continues even after an error is encountered during include function but the execution stops when an error occurs (E_COMPILE_ERROR) while running with the required function.
- Ex: require statement can be used to execute in cases of CMS, Framework, or a PHP function coding where we have to include a key file into the flow of it. This helps in maintaining application integrity and security suppose if one of the key files is not present.
- Similarly, by including the file we can save a lot of time as we can always create and include a standard footer, header, or a menu file for all of the web pages. Then we can update only the header file when it needs to be done.
Advantages of using PHP include_once
- Since this include_once throws a warning when it is not able to find the specific file instead of a fatal error and hence the execution will not break and still goes on smoothly.
- Best in handling cases where we need to reuse some of the functions and variables.
- It helps in avoiding problems on the reassignment of variable values, redefining certain functions, etc.
- Useful in cases where we have to call certain functions periodically.
PHP includes once is used for including any script and to make sure its executed only once hence preventing any errors which might be caused due to duplicate calling of the same. This is basically used where a common piece of code needs to be used for any kind of declarations or config files which are required for the code execution.
This is a guide to PHP include_once. Here we also discuss the introduction and Working of PHP include_once Function along with different examples and its code implementation. you may also have a look at the following articles to learn more –