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?
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?
IMHO there is no real scenario that fits include
and include_once
because of two reasons:
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.
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 theinclude
statement and replaces theinclude
statement with the contents of the file. Conversely, in the case of therequire
statement, therequire
statement is replaced with the contents of the requested file regardless of whether therequire
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
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.
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.include
/include_once
in order to be able to suppress possible include errors using @
operator.