Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-03-2009
Member
 
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
Reply With Quote
  #2  
Old 25-03-2009
Member
 
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).
Reply With Quote
  #3  
Old 25-03-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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.
Reply With Quote
  #4  
Old 25-03-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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.
Reply With Quote
  #5  
Old 25-03-2009
Member
 
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).
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 02:27 PM.