943,992 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 57211
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 16th, 2005
-2

the difference between include and require

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

thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
michael123 is offline Offline
93 posts
since Jun 2005
Jun 16th, 2005
0

Re: the difference between include and require

Quote originally posted by php.net ...
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?
Team Colleague
Reputation Points: 96
Solved Threads: 21
The Geek Father
Zachery is offline Offline
781 posts
since Nov 2003
Jun 16th, 2005
0

Re: the difference between include and require

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!
Reputation Points: 36
Solved Threads: 6
Posting Whiz
Troy is offline Offline
354 posts
since Jun 2005
Jun 25th, 2005
0

Re: the difference between include and require

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.

i.e.
$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.
Reputation Points: 33
Solved Threads: 9
Junior Poster
pclfw is offline Offline
132 posts
since Jun 2005
Mar 4th, 2010
0
Re: the difference between include and require
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pbsanbu is offline Offline
1 posts
since Mar 2010
Aug 12th, 2010
0
Re: the difference between include and require
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
Last edited by mathewmoozh; Aug 12th, 2010 at 3:40 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathewmoozh is offline Offline
15 posts
since Nov 2007
Aug 12th, 2010
0
Re: the difference between include and require
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)
Reputation Points: 13
Solved Threads: 34
Posting Whiz in Training
metalix is offline Offline
218 posts
since Mar 2010
Oct 23rd, 2010
0

when to use require and include

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().
Last edited by Ezzaral; Oct 25th, 2010 at 1:53 pm. Reason: Snipped link. Please restrict such links to your site-wide user signature, which can be edited from the user control panel.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xinam is offline Offline
3 posts
since Oct 2010
Jan 27th, 2011
0

Replay for include and include_once

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathewmoozh is offline Offline
15 posts
since Nov 2007
Jan 27th, 2011
0
Re: the difference between include and require
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.
Reputation Points: 11
Solved Threads: 6
Light Poster
johnsteve.bravo is offline Offline
39 posts
since Jan 2011
Message:
Previous Thread in PHP Forum Timeline: PHP login/logout help
Next Thread in PHP Forum Timeline: Question about Facebook like button





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC