PHP programmers regularly use the functions (or more appropriately known as language constructs) like include
, include_once
, require
and require_once
to insert useful files in the flow of execution. Although these constructs are used quite interchangeably, there are actually significant differences among them. Many programmers don't know well enough about these differences. I hope this post will help them towards a better coding practice.
Difference between include
& include_once
or require
& require_once
:
The difference include vs. include_once and require vs. require_once is obvious from the names. include
and require
allow you to insert a file multiple times within a single execution lifetime. On the other hand, include_once
and require_once
make sure one file is inserted only once in a single execution lifetime, even if your CODE calls them multiple times.
Difference between include
& require
or include_once
& require_once
:
Another significant difference is: include vs. require or include_once vs. require_once. Even though naming of these constructs are almost perfect according to their functionality, many PHP programmers fail to understand the significant difference.
As the name suggests, with include
or include_once
, you simply include other files in the flow of execution. On the other hand, when you use require
or require_once
, you not only include the file in the flow of execution, but also you tell PHP that the execution cannot be continued without that file. In more technical terms:
If PHP interpreter fails to include a file in response to a call from
include
orinclude_once
, a Warning in generated and execution continues without that file. Where as, failure to include a file called byrequire
orrequire_once
generates a Fatal Error and execution stops there.
Sometimes people compare require
with include or die()
. However, that is not entirely correct either. Because include( 'file' ) or die();
will not generate Fatal Error
, but require( 'file' );
will, if the file
is missing or unreadable.
So in a normal scenario, where you want the execution to go on and show users the output, even if the file is accidentally missing or unreadable, use include
or include_once
.
On the other hand always use require
or require_once
to include key files to the flow of execution. This will help avoid compromising your application's security and integrity,ย just in-case one key file is accidentally missing. Then use error_reporting(0) to suppress all the errors and use proper Error Handler to show appropriate information.
What is ideal for view files that are embedded in every other view? include or require?
Usually include is used in view files, unless that part of view is mission critical and the entire site should be stopped from showing anything further if that view is missing. In most cases, this doesn't apply, so include should be fine for any view file.
I think you mean "fatal" error.
A "fetal" error means bad things happen to DNA before we are born!
Ha ha ha, you are absolutely right. Thanks for the Typo correction.
Thank you!
Great article
"require()" is shorthand for "include() or die()" ๐
Nope! Require() also throws fatal error, if failed.
Where as include() or die() will just stop execution if failed. In advanced programming that makes a lot of difference ๐