Saturday, November 16, 2024
Google search engine
HomeLanguagesWhen should one use require_once() vs require() in PHP ?

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

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

RELATED ARTICLES

Most Popular

Recent Comments