GeekInterview.com
Series: Subject: Topic:
Question: 97 of 98

What is difference between require_once(), require(), include().
Becouse above three function usely use to call a file in another file.

Asked by: mrbaliram | Member Since Jan-2006 | Asked on: Feb 4th, 2006

View all questions by mrbaliram   View all answers by mrbaliram

Editorial / Best Answer

Answered by: bicu

View all questions by bicu   View all answers by bicu

Member Since Sep-2005 | Answered On : Feb 4th, 2006

Difference between require() and require_once():  require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).
So, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don't include the file more times and you will not get the "function re-declared" error.

Difference between require() and include() is that require() produces a FATAL ERROR if the file you want to include is not found, while include() only produces a WARNING.

There is also include_once() which is the same as include(), but the difference between them is the same as the difference between require() and require_once().

Showing Answers 1 - 8 of 8 Answers
bicu

Answered On : Feb 4th, 2006

View all questions by bicu   View all answers by bicu

Difference between require() and require_once():  require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).
So, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don't include the file more times and you will not get the "function re-declared" error.

Difference between require() and include() is that require() produces a FATAL ERROR if the file you want to include is not found, while include() only produces a WARNING.

There is also include_once() which is the same as include(), but the difference between them is the same as the difference between require() and require_once().

Yes  5 Users have rated as useful.
  
Login to rate this answer.
babu rao ganpat rao aapte

Answered On : Feb 2nd, 2007

fatal err generated only one time in require_once.
fatal err generated every time in requireand warning generated only one time in require once

  
Login to rate this answer.
veena

Answered On : Nov 1st, 2007

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(). The conditional statement won't affect the require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed.

Yes  3 Users have rated as useful.
  
Login to rate this answer.
ashish.gijare

Answered On : Feb 18th, 2009

View all answers by ashish.gijare

Fatal error is generated only once in "require_once", but the error is repeated in"require" & "include".

  
Login to rate this answer.

require 'file1': It will include the code of the file1. In case file1 does not exist it will generate a fatal error and script will stop executing
further.

require_once 'file1' : It works the same way. The only difference between require and require_once is that require_once does not include and evaluates file again if it has been included earlier.

include 'file1': This will also include and evaluates the file but in case of non
existence of the file it will not generate fatal error. It only produces warning which will not halt the script from executing further.

include_once
'file1': It works the same way. The only difference between include and include_once is that
include_once does not include and evaluates file again if it has been included earlier.


  
Login to rate this answer.
Biswajit769

Answered On : Jul 16th, 2009

View all answers by Biswajit769


include() and require() functions are same to include an external file, but the main difference is how they handle the failure, if the included file doesn't exist include() generate a warning message but require generates a fatal error and script execution stop. Similarly include_once() and require_once() are function to include a file only once to avoid variable redefination but if the file included more than once require_once() produces a fatal error while include_once() produce a warning message.

  
Login to rate this answer.
abc

Answered On : Sep 12th, 2011

require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).

  
Login to rate this answer.
anusha

Answered On : Apr 13th, 2013

require(): displays error and stops the execution.
whereas, include gives a warning and continues the execution.

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.