3

I've read that one of the differences between include and require is that while include only includes the included file if the include statement is encountered, require includes the file into the file hosting the require statement even when the require code is not reached by the execution flow.

How can this have any implications. After all, if I have a file which says:

<?php
  echo __LINE__;
?>

the output will always be 3, instead of printing the line inside at the position inside the file which includes such include file.

6 Answers 6

7

This is simply not true, as far as I know. If the require line is not reached in the code, the file will certainly not be included.

The PHP Manual on require states:

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error.

(emphasis on "except" added by me)

2

As far as I know, that's not how require works. The only practical difference is that require will cause your script to terminate if the file cannot be included, and include will just throw a warning and keep executing.

A very important idiom depends on your stated behavior not being true: conditional required files.

if(is_secure_page()) {
  require "security.php";
}
2

Both require and include include their files at the point where they are reached in the execution flow.

A simple test case for this is:

if (false) {
  require_once("Nonexistent.php");
}
echo "I'm doing SCIENCE and I'm still alive.\n";
1

Let's say you are running a mission critical application and will lose $1 per second. And some libraries are only used in certain case and are not affecting your business flow. You should probably use include_once for these optional libraries. Because if the file is not there require will stop your application from running with a fatal error.

1

require, or even better, require_once should be used exclusively when you want to include code that is actually required by the application, e.g. any libraries you need. include and its brother, include_once should only be used when the included file is not necessary to the execution of your script, such as when you are loading a sidebar or some tangental content.

The only real difference between the two statements is that is the file is not found, require with throw a fatal error, and include will just throw a warning and go on happily chugging away.

I believe that both require and include are loaded and parsed in exactly the same manner. If require would load a file in a given context, so would include.

1

I personally always use require_once, I find in most cases it's what you need (either you get the page once or you halt the script). And include throws warnings that you need to hide if you want to "try" including stuff. But that's just bad programming practice, a simple if (file_exists($file)) require_once $file; does the trick and totally beats include.

So in my opinion, require wins by far.

PS: Beaten by a sec !

1
  • How does this answer the question?
    – kapa
    Jul 10, 2012 at 20:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.