GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  PHP
Go To First  |  Previous Question  |  Next Question 
 PHP  |  Question 2 of 74    Print  
What is difference between require_once(), require(), include().
Becouse above three function usely use to call a file in another file.

  
Total Answers and Comments: 6 Last Update: July 17, 2009     Asked by: mrbaliram 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: veena
 
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.

Above answer was rated as good by the following members:
sangeethasai, jachmullan, prasad_konnur, dineshmatolia, lambedanish313, Harindersingh1985
February 04, 2006 02:58:57   #1  
bicu Member Since: September 2005   Contribution: 21    

RE: What is difference between require_once(), require...

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


 
Is this answer useful? Yes | NoAnswer is useful 4   Answer is not useful 1Overall Rating: +3    
February 02, 2007 12:00:56   #2  
babu rao ganpat rao aapte        

RE: What is difference between require_once(), require...
fatal err generated only one time in require_once.
fatal err generated every time in requireand warning generated only one time in require once

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 3Overall Rating: -3    
November 01, 2007 05:56:17   #3  
veena        

RE: What is difference between require_once(), require...
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.
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 1Overall Rating: +1    
February 18, 2009 10:01:19   #4  
ashish.gijare Member Since: February 2009   Contribution: 7    

RE: What is difference between require_once(), require(), include().Becouse above three function usely use to call a file in another file.
Fatal error is generated only once in require_once but the error is repeated in require & include .
 
Is this answer useful? Yes | No
April 10, 2009 15:32:59   #5  
Harindersingh1985 Member Since: April 2009   Contribution: 2    

RE: What is difference between require_once(), require(), include().Becouse above three function usely use to call a file in another file.
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.



 
Is this answer useful? Yes | No
July 16, 2009 02:46:29   #6  
Biswajit769 Member Since: May 2009   Contribution: 2    

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

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.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape