PHP | include_once() and require_once() - GeeksforGeeks

08 Mar, 2018
We already have learnt about file inclusion in PHP in the article PHP | (Include and Require). We have discussed about include() and require() functions for file inclusion in our previous article. In this article we will discuss about two more yet useful functions in PHP for file inclusion: include_once() and require_once() functions.
include_once() Function
The include_once() function can be used to include a PHP file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions.
If a file named a.php is a php script calling b.php with include_once() function, and does not find b.php, a.php executes with a warning, excluding the part of the code written within b.php.
Syntax:
 include_once('name of the called file with path');

Example:
// name of file is header.inc.php  <?php  echo "GEEKSFORGEEKS";  ?>
The above file is header.inc.php
The above file header.inc.php, is included twice with include_once() function in the following file index.php. But from the output, you will get that the second instance of inclusion is ignored since include_once() function ignores all the similar inclusions after the first one.
// name of file is index.php  <?php  include_once('header.inc.php');  include_once('header.inc.php');  ?>
Output:
GEEKSFORGEEKS
require_once() Function
require_once() function can be used to include a PHP file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions.
If a.php is a php script calling b.php with require_once() function, and does not find b.php, a.php stops execution causing a fatal error.
Syntax:
 require_once('name of the called file with path');

Example:
// name of file is header.inc.php  <?php  echo "GEEKSFORGEEKS";  ?>
The above file is header.inc.php
The above file header.inc.php, is included twice with require_once() function in the following file index.php. But from the output, you will get that the second instance of inclusion is ignored since require_once() function ignores all the similar inclusions after the first one.
// name of file is index.php  <?php  require_once('header.inc.php');  require_once('header.inc.php');  ?>
Output:
GEEKSFORGEEKS
include_once() vs require_once()
Both functions work as same and produce same output but if any error arises then differences come.
Example:
If we don’t have a file named header.inc.php, then in the case of the include_once(), the output will be shown with warnings about missing file, but at least the output will be shown from the index.php file.
In the case of the require_once(), if the file PHP file is missing, then a fatal error will arise and no output is shown and the execution halts.
PHP-basics
PHP-function
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
How to pass JavaScript variables to PHP ?
Split a comma delimited string into an array in PHP
Download file from URL using PHP
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?