The Wayback Machine - https://web.archive.org/web/20240622154339/https://www.geeksforgeeks.org/when-should-one-use-require_once-vs-require-in-php/
Open In App

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

Last Updated : 25 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

By using require(), the file can be included more than once on the same web page. By using require_once(), the file can only be included once on a web page.
Increase the loading time of the web page. The loading time of the web page is minimum.
Increase the complexity of the web page. The complexity of the web page is minimum.

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


Similar Reads

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 of the script. In order to use this require() funct
3 min read
Why require_once() function is so bad to use in PHP ?
The require_once() function is an inbuilt function in PHP which is useful in case if we want to include a PHP file in another one i.e. when we need to include a file more than once in PHP script. It is used to check if the file is included more than once or not as it ignores all the inclusions while running the script if the file is already include
3 min read
Difference between require-dev and require in PHP?
Before going to understand the difference between require and require_dev first understand what is require and require_dev. require: These are must packages for the code to run. It defines the actual dependency as well as package version. require_dev: It defines the packages necessary for developing the project and not needed in production environm
3 min read
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(
3 min read
Describe PHP Include and Require
In this article, we will see what include() &amp; the require() functions is, also will know how these functions affect the execution of the code, their differences &amp; usage in PHP, along with understanding their implementation through the examples. As we know PHP allows us to create various functions and various elements that are used multiple
3 min read
How failures in execution are handled with include() and require() functions in PHP ?
The include() and require() functions use the code or text present in the specified file and copy the code/text into the file which uses the include/require functions. The benefit of using these functions is that it saves time and lines of code because it helps us to reuse the functionalities that are already defined wherever we want by a simple si
3 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 code reusability, along with breaking down large a
3 min read
How to insert PHP code in HTML document using include or require keyword ?
Inserting PHP code in an HTML document involves embedding PHP scripts within HTML files. This is done using PHP tags (`&lt;?php ... ?&gt;`). Common methods include using include or require statements to incorporate external PHP files, allowing dynamic content within the HTML structure. We can insert any PHP file into the HTML code by using two keyw
3 min read
When should one use Arrow functions in ES6 ?
In this article, we will try to understand when should one use Arrow functions in ES6 instead of the traditional function's syntax with the help of some examples. Before analyzing why we should use arrow functions, let us first understand the basic details which are associated with the arrow functions (including the syntax of arrow functions and ma
4 min read
Which one should we use 'border: none' or 'border: 0 '?
In this article, we will learn the validity of CSS border properties. We can specify the no border property using CSS border: none, border-width : 0, border : 0 properties. Syntax: border: none | 0 Differences: border: noneborder : 0For this property, the browser does not render.For this property, the browser renders border-color and border-width p
1 min read