176

Is there any difference between them? Is using them a matter of preference? Does using one over the other produce any advantages? Which is better for security?

4
  • 4
    possible duplicate of When should I use require_once vs include?
    – Gordon
    Sep 3, 2010 at 7:56
  • 3
    Always use "require". "include" is as convenient as an electric door in a sauna. Sep 3, 2010 at 9:50
  • 2
    @MarcoMariani How would that be inconvenient? It's probably clear, I'm just not seeing it. Perhaps the steam? Jun 12, 2014 at 19:44
  • To put it simply, if a 'foo.php' file is missing by mistake, I want to know as soon as possible, not when a function that should have been in foo.php is called. Replacing include with require can often reveal bugs. Let's say config.php is missing, and the application is running with a default configuration. Which is better for security? As for the sauna, when I'm inside and the door does not open for some reason I don't like it. Jun 13, 2014 at 7:55

7 Answers 7

186

require will throw a PHP Fatal Error if the file cannot be loaded. (Execution stops)

include produces a Warning if the file cannot be loaded. (Execution continues)

Here is a nice illustration of include and require difference:

enter image description here

From: Difference require vs. include php (by Robert; Nov 2012)

2
  • 3
    the link is broken Aug 28, 2016 at 17:28
  • No need for the link with the illustration. ;)
    – COil
    Jun 2, 2021 at 18:29
151

You find the differences explained in the detailed PHP manual on the page of require:

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.

See @efritz's answer for an example

5
  • 6
    tiposaurus.co.uk/2011/04/04/… "The key difference between require() and include() is that if you require() a file that can't be loaded (eg if it isn't there) then it generates a fatal error which will halt the execution of the page completely, and no more output will be generated. On the other hand, if you include() a file that can't be loaded, then this will merely generate a warning and continue building the page."
    – stormwild
    Sep 26, 2012 at 1:59
  • 3
    "What one you should use depends on the situation; require() is best suited for loading files that are essential to the rest of the page - for example if you have a database driven website then using require() to include a file containing the database login and password is clearly preferred over using include(). If you used include() in this situation, then you may end up generating more warnings and errors than you had intended."
    – stormwild
    Sep 26, 2012 at 2:01
  • 3
    <?php if (isset($flibbertygibbet)) require 'file.php'; would seem to make this answer look totally incorrect. Otherwise, i should get a fatal error even though the condition isn't true. strace doesn't show PHP even trying to touch file.php.
    – cHao
    Feb 13, 2013 at 17:06
  • 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(). AND However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed. Seem to be saying the opposite thing, or am i misunderstanding?
    – lorless
    Aug 15, 2013 at 16:35
  • 1
    All of these comments are referring to the original version of this answer before it was edited. Originally there was an invalid claim, claiming that if (false) require 'file.php'; would cause 'file.php' to be loaded (but not executed). TLDR disregard all of these comments. Aug 14, 2015 at 5:39
5

Use include if you don't mind your script continuing without loading the file (in case it doesn't exist etc) and you can (although you shouldn't) live with a Warning error message being displayed.

Using require means your script will halt if it can't load the specified file, and throw a Fatal error.

3

The difference between include() and require() arises when the file being included cannot be found: include() will release a warning (E_WARNING) and the script will continue, whereas require() will release a fatal error (E_COMPILE_ERROR) and terminate the script. If the file being included is critical to the rest of the script running correctly then you need to use require().

For more details : Difference between Include and Require in PHP

2

As others pointed out, the only difference is that require throws a fatal error, and include - a catchable warning. As for which one to use, my advice is to stick to include. Why? because you can catch a warning and produce a meaningful feedback to end users. Consider

  // Example 1.
  // users see a standard php error message or a blank screen
  // depending on your display_errors setting
  require 'not_there'; 


  // Example 2.
  // users see a meaningful error message
  try {
      include 'not_there';
  } catch(Exception $e) {
     echo "something strange happened!";
  }

NB: for example 2 to work you need to install an errors-to-exceptions handler, as described here http://www.php.net/manual/en/class.errorexception.php

  function exception_error_handler($errno, $errstr, $errfile, $errline ) {
     throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  }
  set_error_handler("exception_error_handler");   
1
  • At the inclusion level is not a great place to learn that the file you're expecting is not there. Neither problem in your example is one to push off on your website viewers.
    – Kzqai
    Sep 1, 2015 at 14:35
0
<?PHP
echo "Firstline";
include('classes/connection.php');
echo "I will run if include but not on Require";
?>

A very simple Practical example with code. The first echo will be displayed. No matter you use include or require because its runs before include or required.

To check the result, In second line of a code intentionally provide the wrong path to the file or make error in file name. Thus the second echo to be displayed or not will be totally dependent on whether you use require or include.

If you use require the second echo will not execute but if you use include not matter what error comes you will see the result of second echo too.

-2

In case of Include Program will not terminate and display warning on browser,On the other hand Require program will terminate and display fatal error in case of file not found.

1
  • Did you really have to bump a 2 1/2 year old question to repeat what 2/3 of the answers have already said...?
    – cHao
    Feb 13, 2013 at 19:06

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