Reputation: 10830
I wanted to see if there was any reason beyond needing it or requiring it (hence the names). I stumbled upon this statement:
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, useinclude()
. The conditional statement won't affect therequire()
. However, if the line on which therequire()
occurs is not executed, neither will any of the code in the target file be executed.
The way I am interpreting it is that includes inside conditionals that do not end up running, will not actually include it in there, which to me seems less costly to the server. I am just curious if this is true. I did not see it in the manual, so I am skeptical.
Upvotes: 3
Views: 91
Reputation: 360572
not exactly how it's implemented, but this should give you an idea of what the difference between the two is:
function require($file) {
if (!is_readable($file)) {
die("can't read $file");
}
include($file);
}
include reads in the file everytime you tell PHP to include it. the _once variants simply keep track of what's been included and only do the include once.
Upvotes: 1
Reputation: 9359
If you have include inside a false statement, it will not get parsed. The same is true for require. The main and only difference is that require will check if the file exists even if it's never executed. Well, this has been my experience with it, at least.
So, both only happen once executed, but require will show an error if the file doesn't exist, regardless of whether it's executed.
Upvotes: 4