-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:

2
  • 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. Commented Feb 10, 2016 at 13:47
  • only advantage of require is microspeed, i can't imagine where i would use require Commented Feb 10, 2016 at 13:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.