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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
Syntax:
include_once('file name with its full path');
The upper syntax is self-explanatory.
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.
Following are the examples as given below:
Code:
<?php // 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.
<?php include_once('Main.php'); include_once('Main.php'); ?>
Output:
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:
FIRST_FUNCTION.php
SECOND_FUNCTION.php
THIRD_FUNCTION.php
The code of FIRST_FUNCTION.php looks like the below:
<?php // declaring a function function func(){ echo 'first function called'; } ?>
SECOND_FUNCTION.php:
<?php // using include function to include the above function include('FIRST_FUNCTION.PHP'); echo 'second function called'; func(); ?>
THIRD_FUNCTION.php
<?php include('FIRST_FUNCTION.PHP'); include('SECOND_FUNCTION.PHP'); func(); ?>
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:
<?php // using include_once function include_once('FIRST_FUNCTION.PHP'); include('SECOND_FUNCTION.PHP'); ?>
Output:
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 the 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:
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.
The above is the detailed content of PHP include_once. For more information, please follow other related articles on the PHP Chinese website!