Take the 2-minute tour ×

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, 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 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.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
Ahh, I see. Thank you very much –  Andy Apr 22 '12 at 4:52
@Andy you should probably upvote and accept the answer. –  ricochet1k Apr 22 '12 at 4:56
add comment

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 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.

share|improve this answer
add comment

Your Answer


