Include VS Require Functions in PHP

by Hiroshi on June 16, 2008

in Include, Require

As clearly describes, the two constructs are identical in every way except how they handle failure.

include() produces a Warning while 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. Be warned that parse error in required file doesn’t cause processing halting in PHP versions prior to PHP 4.3.5.

Using ‘REQUIRE’ will ensure that the file is included as the parser looks for ‘REQUIRE’s prior to parseing the file. Thus ‘REQUIRE’d files are always included. However the ‘INCLUDE’ may be avoided via programming.

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.

Related Posts

Previous post:

Next post: