Results 1 to 5 of 5

Thread: include() v/s require()

  1. #1
    Join Date
    Feb 2009
    Posts
    64

    include() v/s require()

    hello,

    What is the difference between include() function and require() function in PHP ?
    Both the function seems to do the same job, so when to use what ?
    If anyone has any ideas regarding this.........please help

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: include() v/s require()

    You can insert the content of a file into a PHP file before the server executes it, with the include() or require() function.
    The two functions are identical in every way, except how they handle errors.

    include() function - generates a warning (but the script will continue execution) while
    require() function - generates a fatal error (and the script execution will stop after the error).

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: include() v/s require()

    The include() statement includes and evaluates the specified file. This also applies to require().
    The two constructs are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, 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.

    Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/. If filename begins with ./ or ../, it is looked for only in the current working directory or parent of the current working directory, respectively.

    When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: include() v/s require()

    require() is identical to include() except upon failure it will produce a fatal E_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.

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: include() v/s require()

    The two functions are used to create functions, headers, footers, or elements that can be reused on multiple pages.

    This can save the developer a considerable amount of time. This means that you can create a standard header or menu file that you want all your web pages to include. When the header needs to be updated, you can only update this one include file, or when you add a new page to your site, you can simply change the menu file (instead of updating the links on all web pages).

Similar Threads

  1. What does KDE 4 desktop include?
    By Bryn in forum Operating Systems
    Replies: 3
    Last Post: 08-11-2010, 06:39 PM
  2. How to use include() in PHP?
    By Emily123 in forum Software Development
    Replies: 6
    Last Post: 06-03-2010, 05:49 AM
  3. How to Use the #include Directive in ASP?
    By NIcaBoy in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 06:24 AM
  4. Include explicitly a jsp page in another
    By MAHESA in forum Software Development
    Replies: 5
    Last Post: 02-12-2009, 08:37 PM
  5. How to include files in PHP
    By Samir_1 in forum Guides & Tutorials
    Replies: 2
    Last Post: 01-07-2009, 11:39 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,627,816.75038 seconds with 16 queries