Difference between include() and include_once() in PHP
Last Updated :
07 Aug, 2024
The include() function in PHP is mostly used to include the code/data of one PHP file to another file. During this process if there are any kind of errors then this require() function will display/give a warning but unlike the require() function in which the execution comes to a halt, the include() function will not stop the execution of the script rather the script will continue its process.
In order to use the include() function, we will first need to create two PHP files. Then using the include() function put one PHP file into another one. After that, you will see two PHP files combined into one HTML file. This include() will not see whether the code is already included in the specified file, rather it will include the code number of times the include() is been used.
Example: Assume we have a file called includegfg.php.
includegfg.php
<?php
echo "
<p>Visit Again; " . date("Y") . " Geeks for geeks.com</p>
";
?>
We have created a file demo.php. Using the include() method we will include the includegfg.php file into the demo.php file.
demo.php
<html>
<body>
<h1>Welcome to geeks for geeks!</h1>
<p>Myself, Gaurav Gandal</p>
<p>Thank you</p>
<?php
include 'includegfg.php';
?>
</body>
</html>
Output:
include_once():
The include_once() function in PHP is mainly used to include one PHP file into another PHP file. It provides us with a feature that if a code from a PHP file is already included in a specified file then it will not include that code again. It means that this function will add a file into another only once. In case this function locates an error then it will produce a warning but will not stop the execution.
If ABC.php file calls XYZ.php file using include_once() and any error occurs then it will produce a warning but will not stop the script execution.
Example: Below we have created a sample PHP file called demo.php, which displays the message "Hello from Geeks for Geeks."
demo.php
<?php
echo "Hello from Geeks for Geeks";
?>
In the following PHP file require_once_demo.php, we have called the demo.php file twice using the require_once(), but it will not execute the second call.
require_once_demo.php
<?php
include_once('demo.php');
include_once('demo.php');
?>
Output:
Difference between include() and include_once():
include() | include_once() |
The include() function is used to include a PHP file into another irrespective of whether the file is included before or not. | The include_once() will first check whether a file is already included or not and if it is already included then it will not include it again. |
This include() function is mainly used where you want to include a certain code again and again. | This include_once() function is mainly used where you want to include a certain code just for once. |
The include() function will execute every time it is called in the program. | The include_once() function will not execute every time it is called (ie. It will not execute if the file to be included is included before) |
Mostly include() function is used to load optional template-like files. | Mostly include_once() function is used to load optional dependencies (classes, functions, constants). |
Similar Reads
Difference between #include and #include" " in C/C++ with Examples Pre-requisites: Header files in C/ C++ and its uses The difference between the two types is in the location where the preprocessor searches for the file to be included in the code. #include <filename> // Standard library header #include "filename" // User defined header #include<filename
4 min read
Difference between require() and include() in PHP In this article, we will see the require() and include() Functions in PHP, along with understanding their basic implementation through the illustration. Both require() and include() Functions are utilized to add the external PHP files in the current PHP script. With this, both functions enhance the
3 min read
Difference between die() and exit() functions in PHP PHP exit() Function: In PHP, the exit() function prints a message and exits the application. It's often used to print a different message in the event of a mistake. Use exit() when there is not an error and have to stop the execution. Syntax: exit("Message goes here"); or exit(); Example: exit("This
2 min read
What's the difference between
and
in PHP ? There are many types of line-ending characters that are used in PHP. They differ depending upon the operating system and editors using them. It's also based on how apps, libraries, protocols, and file formats deal with things. These characters are invisible. \n is used for the newline or linefeed, w
2 min read
Difference between require() and require_once() in PHP PHP require() Function: The require() function in PHP is mostly used to include the code/data of one PHP file to another file. During this process, if there are any kind of errors then this require() function will display a warning along with a fatal error which will immediately stop the execution o
2 min read
Difference between $var and $$var in PHP In PHP, $var is used to store the value of the variable like Integer, String, boolean, character. $var is a variable and $$var stores the value of the variable inside it. $var: Syntax: $variable = value;The $variable is the variable nameThe value is the initial value of the variable. Example 1: This
2 min read