When should one use require_once() vs require() in PHP ? - GeeksforGeeks

25 Aug, 2021
In this article, we will learn, when should one use the require_once() method and when to use require() method in PHP.
require() Method: PHP require() is a library or built-in function in PHP. It is generally used in situations where we want to include a file in the PHP code or program. The require() method will throw a fatal error if the file on the path specified is not found. It will also stop the execution of the content in case if any error occurs. By using require() users can include a file in a particular PHP code as many times as they want.
Syntax:
require('File_Name_With_Extension');
 
Example: The following file demonstrates the PHP require() method. It includes the “welcome.html” file content given below.
require.php
<?php          require('welcome.html');    require('welcome.html');       ?>
The following is the content for file “welcome.html” used in the above PHP code.
welcome.html
<!DOCTYPE html><html>  <body>    <p>This file is included.</p>  </body></html>
After running the “require.php” file,the following result is shown.
Output: 
This file is included.
This file is included.
require_once(): PHP require_once() is also a library function in PHP. It is also used in a situation where we want to include a file in the PHP code or program. The require_once() will also throw a fatal error if the file on the path specified is not found. It will also stop the execution of the content in case any error occurs. By using require_once(), the user can include a file for ONCE in a particular PHP code.
 
Syntax:
require_once('File_Name_With_Extension');
Example: In the below example I have created two files i.e. “welcome.html” and “require_once.php”. The require_once() has been called twice and the file “welcome.html” is included only once. 
require_once.php
<?php     require_once('welcome.html');       require_once('welcome.html'); ?>
welcome.html
<!DOCTYPE html><html>  <body>    <p>This file is included only once!</p>  </body>  </html>
After running the “require_once.php” file,the following result is shown,
Output: 
This file is included only once!
Difference between require() and require_once():
require()
require_once()
When should one use require_once vs. require?
The require() includes the files irrespective of whether the file has already been included within the web page or not. So, one can use the require() in case if you want to include the file contents again and again and are only concerned with delivering the output on the web page.
Whereas require_once() will include the file only once on the web page even if the function is called twice it will be executed only once while ignoring the second function call which will further result in minimization of loading time and complexity of the web page. So if you are concerned with delivering the output along with the loading time and complexity of the web page one should definitely use require_once().
PHP-function
PHP-Questions
Picked
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to fetch data from localserver database and display on HTML table using PHP ?
How to create admin login page using PHP?
PHP str_replace() Function
How to pass form variables from one page to other page in PHP ?
Create a drop-down list that options fetched from a MySQL database in PHP
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript