Difference between "include" and "require" in php

122,283

Solution 1

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

Solution 2

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)

Solution 3

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.

Solution 4

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

Solution 5

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");   
Share:
122,283

Related videos on Youtube

Dan Hanly
Author by

Dan Hanly

My name is Dan Hanly and I am a Full Stack Application Developer from Pontypridd in South Wales. I have a tremendous passion for developing in a range of languages and for bringing large-scale projects through their full development lifecycle into a production environment. I am highly capable and a quick learner, always bringing fresh ideas, out-of-the-box thinking and the ability to adapt my professional skills to any project requirement. I’m not constrained by technology, understanding instead the need to adopt the appropriate technology for each unique project. I am also keenly involved in the Open Source community, developing useful components and trying to involve myself in large projects in need of development help. My strengths are: Great at tackling complex problems I write clean, well-commented code Experienced with a range of testing (TDD & BDD) I follow development standards Experienced with task-automation Working in an Agile development environment Utilising git version control on all projects

Updated on January 08, 2021

Comments

  • Dan Hanly
    Dan Hanly over 3 years

    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?

    • Gordon
      Gordon over 13 years
    • Marco Mariani
      Marco Mariani over 13 years
      Always use "require". "include" is as convenient as an electric door in a sauna.
    • Austin Burk
      Austin Burk almost 10 years
      @MarcoMariani How would that be inconvenient? It's probably clear, I'm just not seeing it. Perhaps the steam?
    • Marco Mariani
      Marco Mariani almost 10 years
      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.
  • stormwild
    stormwild over 11 years
    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
    stormwild over 11 years
    "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."
  • cHao
    cHao about 11 years
    <?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
    cHao about 11 years
    Did you really have to bump a 2 1/2 year old question to repeat what 2/3 of the answers have already said...?
  • Dan Hanly
    Dan Hanly about 11 years
    Aww, he's a newbie! Probably doesn't understand the concept of SO yet.
  • lorless
    lorless over 10 years
    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?
  • Kevin Wheeler
    Kevin Wheeler over 8 years
    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.
  • Kzqai
    Kzqai over 8 years
    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.
  • Michael Okoli
    Michael Okoli over 7 years
    the link is broken
  • Gagantous
    Gagantous over 5 years
    poor user2069222, now he left SO :'(
  • COil
    COil almost 3 years
    No need for the link with the illustration. ;)