PHP | include_once() and require_once()

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// name of file is header.inc.php
  
<?php
  
echo "GEEKSFORGEEKS";
  
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// name of file is index.php
  
<?php
  
include_once('header.inc.php');
  
include_once('header.inc.php');
  
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// name of file is header.inc.php
  
<?php
  
echo "GEEKSFORGEEKS";
  
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// name of file is index.php
  
<?php
  
require_once('header.inc.php');
  
require_once('header.inc.php');
  
?>

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.