views:

49

answers:

2

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.

+1  A: 

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

tandu
+1  A: 

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)

Spudley
IIRC, the behaviour he describes used to be the case until PHP 4.something.
Pekka
Oh, I am actually using require_once, I just simplified for the sake of the question. But thanks
Jens Roland
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 :)
Jens Roland
@Pekka - I'm fairly sure it wasn't the case in PHP4 either. [edit] Yep, I got that just after I posted :)
Spudley