What is different between include and require in PHP?

Discussion in 'PHP' started by SEO_BOSS, Oct 13, 2011.

  1. #1
    Hi friends,


    What is different between include and require in PHP?
     
    SEO_BOSS, Oct 13, 2011 IP
  2. Good Point

    Good Point Well-Known Member

    Messages:
    287
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    123
    #2
    include and require simply generate different errors and depending on your php settings php will handle that error differently.

    For example, if you were to include/require a file that does not exist:
    Include will allow the script to continue, generating a warning error.
    Require will halt and generate a fatal error, disallowing the script to continue, hence the name require, meaning the script cannot run without the required file.

    :)
     
    Good Point, Oct 13, 2011 IP
  3. prince@life

    prince@life Notable Member

    Messages:
    278
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    225
    #3
    mine suggestion is to use include statements ...
     
    prince@life, Oct 17, 2011 IP
  4. alimoula

    alimoula Peon

    Messages:
    114
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i also suggest to use include. its more formal.....
     
    alimoula, Oct 17, 2011 IP
  5. DavidWincent

    DavidWincent Peon

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    "Require" ensures that the file is included. Hence, by using "require" the parser checks that the files are always included. This is not the case with "include" and it may be avoided.
     
    DavidWincent, Oct 18, 2011 IP
  6. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #6
    Require gives you a fatal error in php if your file isn't found, include gives you a warning. Require is also more CPU intensive.
     
    Einheijar, Oct 18, 2011 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #7
    you also have include_once and require_once

    I recommend you get familiar with www.php.net which is reliable and instant for simple explanations like this.
     
    sarahk, Dec 9, 2011 IP
  8. jamesfletcher

    jamesfletcher Greenhorn

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #8
    From PHP manual : require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, don’t hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well. I think its enough information to resolve your question.
     
    jamesfletcher, Dec 10, 2011 IP
  9. Jaxo

    Jaxo Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #9
    In my opinion, you should ALWAYS use require(). If you want a file to be present inside another file, you want it to either work or not work. It's like having this:

    File 'includeme.php':
    $variable='my variable';
    PHP:
    File 'index.php':
    include('includeme.php');echo $variable;
    PHP:
    You may look at this and see nothing wrong. However, if includeme.php fails to include properly, like the file has moved or it's permissions are weird, index.php won't display 'my variable' as it should. require(); will exit() the script with a fatal error, which is better than having a faulty webpage. At least the error will tell you what happened.

    You can try this for yourself by setting error_reporting to E_ALL (like this:)
    error_reporting(E_ALL);
    //That's all you need
    PHP:
    PHP will generate notices if a variable isn't found, so with E_ALL on, this script will return a notice of $var not found
    echo $var;
    //But where is $var?
    PHP:
    helping you seek out the issue.
     
    Jaxo, Dec 14, 2011 IP