tags:

views:

566

answers:

9

When should I use require_once vs include?

(The code is in a custom wordpress theme if that matters)

+2  A: 

If you don't have output done by the file, you should use require.

If you have output done by the file, use include.

The once can be used if you want to include or require the resources only once. For example if you store settings a php file, you use require_once on those, as you need it only once.

Pentium10
What difference does output make?
Javier Badia
The contents of the file are added to the output. For example I can include('header.html'); and the content is included, if I use require the contents are not copied to output.
Pentium10
+18  A: 

There are require and include_once as well.

So your question should be...

  1. When should I use require vs. include?
  2. When should I use require_once vs. require

The answer to 1 is described here.

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

The answer to 2 can be found here.

The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

Mef
for example.....
Muhammad Sajid
+6  A: 

Difference between _once functions and without _once functions: without _once code will be included again whereas with _once functions PHP keeps track of the included files and will include it only once.

Difference between require and include: If a required file is not found PHP will emit a fatal error whereas for include only a warning will be emitted.

Sebastian
+1  A: 

The difference is in the error the commands generate. With require, the file you want to use is really required and thus generates an E_ERROR if it is not found.

require() is identical to include() except upon failure it will also produce a fatal E_ERROR level error.

include only generates an E_WARNING error if it fails which is more or less silent.

So use it if the file is required to make the remaining code work and you want the script to fail the file is not available.


For *_once():

include_once() may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.

Same applies to require_once() of course.


Reference: require(), include_once()

Felix Kling
+1  A: 

With require the file must exist, if it doesn't then an error will display; whereas with include - if the file doesn't exist then then the page will continue loading.

Brian
+4  A: 

Use

  • require
    when the file is required by your application, e.g. an important message template or a file containing configuration variables which with without the app would break.

  • require_once
    when the file contains content that would produce an error on subsequent inclusion, e.g. function important() { /* important code */} is definitely needed in your application but since functions cannot be redeclared should not be included again.

  • include when the file is not required and application flow should continue when not found, e.g.
    great for templates referencing variables from the current scope or something

  • include_once
    optional dependencies that would produce errors on subsequent loading or maybe remote file inclusion that you do not want to happen twice due to the HTTP overhead

But basically, it's up to you, when to use which.

Gordon
+1  A: 

My suggestion is to just use require_once 99.9% of the time.

Using require or include implies that your code is not reusable elsewhere, i.e. that the act of using require or include is actually executing code instead of making available a class or some function libraries. If you are require/including code that does that, that's procedural code, and you need to get to know a new paradigm. I suggest functional programming and object oriented programming, in that order.

If you're already doing functional programming, using include_once is mostly going to be delaying where in the stack you find bugs/errors. Do you want to know that the function do_cool_stuff() is not available when you go to call for it, or the moment that you expect it to be available? Generally, the latter, so just use require_once.

Tchalvak
A: 

require critical parts, like authorization and include all others.

multiple includes is just very bad design, and must be avoided at all. so, *_once doesn't really matter.

Col. Shrapnel
A: 

"include" for reusable php-templates. "require" for required libs.

"*_once" is nice, because it checks whether the file is already loaded or not. but for me it makes only sence for in "require_once"

mo