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

Questions by mrbaliram   answers by mrbaliram

Editorial / Best Answer

bicu  

  • Member Since Sep-2005 | 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 - 28 of 28 Answers

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().

babu rao ganpat rao aapte

  • 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

  Was this answer useful?  Yes

veena

  • 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.

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.


  Was this answer useful?  Yes

Biswajit769

  • Jul 16th, 2009
 


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.

  Was this answer useful?  Yes

abc

  • 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).

  Was this answer useful?  Yes

anusha

  • Apr 13th, 2013
 

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

  Was this answer useful?  Yes

LAILA NISHI

  • Feb 21st, 2015
 

Include() and require() dose same work.The difference is on error cheeking .Require () gives a fatal error when file cant load whereas include() gives a warning and continue to execute.

  Was this answer useful?  Yes

aditya dhanraj

  • Apr 29th, 2015
 

require_once():-using this function we can access the data of another page once i. e we can not access the same page multiple times.
require():-using this function we can access the data of another page at the desire page multiple times i.e we can access the contents of same page multiple times.

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions