What's the difference between "include", "include_once", "require", "require_once"?


The documentation below also applies to require(). The two constructs are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well. Be warned that parse error in required file doesn't cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does.

That cover it?

Yep, the documentation at www.php.net makes it pretty clear. If you code PHP, you probably want to keep php.net open in a browser at all times! Very nice documentation, and the comments can be a lifesaver---when they are accurate anyway!

In my applications, and I have to think in most people's, my includes are not optional. And of course, I only want an include file included once. So I almost always use require_once().

I was a hard-core ASP developer for many years. It took me all of 2 weeks to become a PHP convert. Features like require_once() and the ability to conditionally include files are part of what makes PHP so sweet when compared to ASP.

Enjoy the journey! :)

Don't forget that using 'REQUIRE' will ensure that the file is included as the parser looks for 'REQUIRE's prior to parseing the file. Thus 'REQUIRE'd files are allways included. However the 'INCLUDE' may be avoided via programming.

$language = 'english';

if $language = "french" {
include_once '.\french_file.php';
else {
include_once '.\not_french_file.php';
In this instance the file to be included will depend on the status of the variable '$french'.

If this was written as -
if $language = "french" {
require_once '.\french_file.php'
else {
require_once '.\not_french_file.php'
then BOTH files would be included, irrespective of the value of $language.

Unlike include(), require() will always read in the target file, even if the line it's on never executes. If you want to conditionally include a file, use include(). The conditional statement won't affect the require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed.

The difference is that if we use require_once() then the file to be added should exist in the given path,if not it will display a fatal error.if we use a include_once there will be no more problem due to this ,if there exist no such file.if we need to include a file then we will prefer include_once() because it will include the file once,it willnot include the file if it is inluded earlier

Edited 6 Years Ago by mathewmoozh: n/a

Wow this is an old thread.

include/include_once will just insert the code in the script.
this will produce a warning if it dies (file does not exist)

require/require_once will also insert the code but will result in a fatal error if dies. (script will terminate)

when you use require then you must gurantee that the file exist otherwise the processing will halt and result to fatal error.
If you need the code to run instead the specified file is missing then better use require().

Edited 6 Years Ago by Ezzaral: Snipped link. Please restrict such links to your site-wide user signature, which can be edited from the user control panel.

Dear Michel,
The difference between require and include is that
for include the file is not mandatory.the program will run even if there is no such a file producing a warning.
But for require the file should exist.otherwise it will produce an error.the program cannot continue

lets look at the include and include_once
we can include a file many time as we uses it anywhere. but there is no need to include a file more than once.
include function will include the file as many times it is used
but include_once will include the file only once if the file exists

the same difference is there in the case of require and require_once

The basic difference is that the files included by using require function would be included whether or not that statement is being executed but if you use include function, then only when the function is executed on run time, then the file would be included otherwise it would not. Include is mainly used when there are too many conditional inclusions in a document and all these files can collectively make the page load slow.

It’s how they handle failures. If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.
Check this link...
It might be useful for you.

Edited 4 Years Ago by Nick Evan: spam removed

This article has been dead for over six months. Start a new discussion instead.