4

Among include, include_once, require and require_once I always just use require_once. Many third-party frameworks just use require_once as well.

Can anybody please describe a real scenario that another construct must be used?

cmbuckley
  • 40,217
  • 9
  • 77
  • 91
Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173
  • I guess that when it comes down to performance include() or require() is a tiny bit faster as it doesn't require any memory comparison. What do you say? – jtheman Nov 13 '12 at 12:09
  • 1
    possible duplicate of [When should I use require\_once vs include?](http://stackoverflow.com/questions/2418473/when-should-i-use-require-once-vs-include) – NullPoiиteя Nov 03 '13 at 04:50

3 Answers3

5

IMHO there is no real scenario that fits include and include_once because of two reasons:

  1. It's highly unlikely that your intention is to include a file and at the same time you don't really care if it's included (e.g. if the file does not exist and execution continues).
  2. Even if that is the case, include will emit a warning which is bad style (zero-warning code is a good thing to strive for). You can prevent this most of the time with a check like is_file, but then you know that the file does exist so why not require it?

For require vs require_once: if a file can legitimately be parsed more than once (e.g. an HTML template) use the former. If it brings code inside your application (the vast majority of cases) use the latter.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • There's an anonymous and useful comment about `is_file` in the php manual `Note that is_file() returns false if the parent directory doesn't have +x set for you; this make sense, but other functions such as readdir() don't seem to have this limitation. The end result is that you can loop through a directory's files but is_file() will always fail.` Source: http://us2.php.net/manual/en/function.is-file.php#107403 – pablofiumara Nov 13 '13 at 03:20
2

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.

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 only difference between the include/require and include_once/require_once statements is how many times a given file will actually be loaded. When the include_once/require_once statements are used, the file cannot be loaded or executed multiple times. If an attempt is made to load a file twice using one of these two methods, it will be ignored. Because it is unacceptable to define the same function multiple times within a script, these functions allow the developer to include a script as needed without having to check whether it has been previously loaded.

<?php

    include ('library.inc');      
    $leap = is_leapyear(2003);
 

    require ('library.inc');     
    $leap = is_leapyear(2003);

?>

If both statements will allow the current script to execute the code in a separate file, what is the difference between the two?

There are two major differences:

the first is the capability to return values and the second is under what circumstances the requested file is loaded. When an include statement is used, PHP delays the actual loading of the requested file until the script reaches the point of executing the include statement and replaces the include statement with the contents of the file. Conversely, in the case of the require statement, the require statement is replaced with the contents of the requested file regardless of whether the require statement (and thus the contents of the file) would have executed in the normal progression of the script.

Quoting above paragraph from http://82.157.70.109/mirrorbooks/php5/067232511X/ch01lev1sec8.html

NOTE

The capability to return values from external files is limited only to the include and include_once statements. The require and require_once statements cannot be used in this fashion.

require give Fatal error but include give Warning

Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • Your final note there; pertaining to the use of `return` is incorrect. `require` can `return` values as `include` does. – Dan Lugg Nov 13 '12 at 13:58
  • 1
    Well, http://php.net/manual/en/function.require.php doesn't mention not supporting it, and a quick test script concludes it work as `include` does. – Dan Lugg Nov 13 '12 at 16:35
  • @Reza it doesnt make any sense to just tell .... provide information about relevant part you think is wrong also provide link which is say the part is wrong – NullPoiиteя Nov 15 '12 at 08:49
  • Most of this answer is a copy-paste from http://82.157.70.109/mirrorbooks/php5/067232511X/ch01lev1sec8.html -- I think it's better to cite your source in the answer :) – Amal Murali Nov 02 '13 at 22:04
  • @AmalMurali it doesnt count most of answer and because it has ip address so doesnt allow to quote that as link .. although this question is dup of http://stackoverflow.com/questions/2418473/when-should-i-use-require-once-vs-include – NullPoiиteя Nov 03 '13 at 04:49
-1

The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal E_COMPILE_ERROR level error.

The same difference applies also for include_once and require_once.

When use include and when use require, is described very good in answers to Difference between require, include and require_once?

From the my point of view, there are 2 things.

  1. if you have something, which is absolutely required to be present in your application, you should use require/require_once to include such definitions. That way you'll get fatal errors instead of warnings and during development that will simplify finding problematic code.
  2. if you're including external resource or something which may be missing, it should be included with include/include_once in order to be able to suppress possible include errors using @ operator.
Community
  • 1
  • 1
bhovhannes
  • 5,511
  • 2
  • 27
  • 37