-1

In PHP, a require statement will include and evaluate the specified file.

require 'path/to/some/file.php';

A require_once statement does the same but will check if the file has already been included and, if so, not include it again.

require_once 'path/to/some/file.php';

The benefit of using require_once is very clear but what's the advantage of using a require statement instead of require_once?

Ref:

henrywright
  • 10,070
  • 23
  • 89
  • 150
  • 5
    For one thing you might want to require a file more than once? It depends on the scenario and what the required file contains / does. – Jonnix Feb 10 '16 at 13:47
  • only advantage of ``require`` is microspeed, i can't imagine where i would use ``require`` – arma Feb 10 '16 at 13:55

3 Answers3

3

That clearly depends on the contents of the file you're requiring and whether it should only be included once or explicitly again and again every time you request it; think partial templates as an example for where you might load the same file multiple times.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).

So, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don't include the file more times and you will not get the "function re-declared" error.

Sinto
  • 3,915
  • 11
  • 36
  • 70
0

Some good info related to your question on this thread.

Specifically on the advantage of using require() over require_once(): require() executes ever so slightly faster so it could be beneficial to use if you're sure there are no duplicate requirements

Community
  • 1
  • 1
alex9311
  • 1,230
  • 1
  • 18
  • 42