24

If I place an include or require statement inside a conditional that evaluates to false, will the PHP interpreter skip the include file altogether, or will it load it just in case?

An example would be:

if ($some_user_var) {
    require 'this.php';
} else {
    //do stuff
}

I read somewhere that require will always be included by the interpreter regardless of the conditional, but include will not. If that's the case, just switching from require to include could mean a free speedup due to the reduced I/O and parsing overhead.

It probably makes a difference if I'm running a preprocessor like eAccelerator, but let's assume I don't.

2
  • I think that what you read somewhere was something like this: require will abort execution with fatal error while include will not (on failure, of course). Which can be interpreted like require will always include your file or the script will die. Commented Oct 11, 2010 at 13:39
  • 1
    Do you by any chance own this book? amazon.co.uk/PHP-World-Wide-Web-Quickstart/dp/0321245652 I was taught this lie as well! Commented May 14, 2011 at 18:21

3 Answers 3

38

It will only be included if the condition is true. I don't know where you read otherwise, but they're wrong.

The only difference between include and require is that include will throw a warning if it fails, whereas require will throw a fatal error.

To confirm this, see the PHP manual page for require.

(ps - if you're doing conditional includes, depending on what the reaon is, you may consider using include_once() or require_once() instead)

Sign up to request clarification or add additional context in comments.

4 Comments

IIRC, the behaviour he describes used to be the case until PHP 4.something.
Oh, I am actually using require_once, I just simplified for the sake of the question. But thanks
Thanks for the link to the manual page. From one of the comments: "As the manual states require and require_once as of PHP 4.02 no longer call the file if the line of code it is on should not be executed". Perfect :)
@Pekka - I'm fairly sure it wasn't the case in PHP4 either. edit Yep, I got that just after I posted :)
2

This is not correct. require will not include files that are wrapped in blocks where they are never called, the php interpreter does not ignore them. include and require have little to no difference performance-wise (for that matter neither do they have much of a difference from _once, though it is more significant).

Comments

1

I read that somewhere too. The argument goes something like this:

If you put a condition around an include, PHP has no way of knowing if it is required or not until it starts to interpret the code and it can't interpret the code until it gets hold of all the variables and functions and therefore - files. So it LOADS UP all the files regardless of the condition and then drops them back out of the final "compilation".

Although then again, if you wrap a condition around a PHP file with an error in it, it doesn't break it. And if you declare a variable in an included file and then use the value of variable to determine whether or not to include it, it doesn't pick up its value:)

Maybe this used to be a problem in old versions of PHP?

I haven't tested it with regards to load speed and RAM usage - but I'd love to get a definitive answer to this. Is there ANY overhead involved with conditional includes?

1 Comment

Good observation about including files with errors in them. If it was preloading them you'd be able to test that by including an explosively bad file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.