Open In App

Difference between include() and include_once() in PHP

Last Updated : 29 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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).


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads