Skip to main content
IBM  
Shop Support Downloads
IBM Home Products Consulting Industries News About IBM
IBM developerWorks : Web Architecture : Education - Tutorials
Writing efficient PHP
ZIPPDF (letter)PDF (A4)e-mail
Main menuSection menuFeedbackPreviousNext
3. Tweaking PHP code
  


Understanding the difference between require and include page 5 of 11


According to the PHP manual, require and include "are identical in every way except how they handle failure." However, further reading of the manual suggests another very subtle difference that impacts performance.

When you use the require keyword, the named file is read in, parsed, and compiled when the file using the require keyword is compiled. When a file containing the include keyword is compiled, the named file is not read in, parsed, and compiled initially. Only when that line of code is executed is the file read, parsed and compiled.

Only use the require keyword if you know you will always need that named file in the current script. If you might use its functions, use include instead. PHP opens up all files that are required, but only opens included files as needed.

Additionally, you should also consider using require_once and include_once in place of require and include respectively. In practice, it is more likely that you actually want the functionality provided by the require_once and include_once functions, even though it is much more common to use the require and include keywords respectively.

Refer to the following PHP manual pages for more information: include, include_once, require, require_once.


Main menuSection menuFeedbackPreviousNext
Privacy Legal Contact