What is the difference between require() and include() in PHP?

Gregorio

New member
Registered
Joined
Dec 3, 2012
Messages
13
Points
0
I start learning PHP but feeling difficult in using these functions. What is the difference between require() and include()?

When should I use require() and When should I use include()?
 

savidge4

Well-known member
Registered
Joined
Jan 6, 2016
Messages
108
Points
0
Technically speaking they are about one in the same. "includes" is a bit more forgiving in terms of error emitting. "require" will stop cold with an error, and "includes" will displace a warning and continue on its way.
 

projectpop

Member
Registered
Joined
Jan 12, 2016
Messages
30
Points
0
More specifically, require will produce fatal error and stop the script on error, script will not continue.
Include will only produce warning, and script will continue.
 

acquisto

Member
Registered
Joined
Jul 17, 2015
Messages
23
Points
0
Use include if you don't mind your script continuing without loading the file (in case it doesn't exist etc) and you can (although you shouldn't) live with a Warning error message being displayed.

Using require means your script will halt if it can't load the specified file, and throw a Fatal error.
 

EpicGlobalWeb

Well-known member
Registered
Joined
Jan 24, 2016
Messages
180
Points
0
Yes there is quite a big difference. For require() Your script will not run until the required elements are included. With include, the remainder of your script will run if the included element fails to load for some reason. I typically use require for database work, security, or payment processing. Include still suits most purposes, however. And in many cases this is down to personal preference.

A programmatic analogy is like this:

In require, if required fails to load then die().

In include, if include fails to load then continue.
 

richasharma

New member
Joined
Feb 3, 2016
Messages
7
Points
0
Both are used for performing same work i.e. loading any php file into another php file
but in require if an error occurs then it will stop scripting their.
In include it will show the warning and script the next code.
 

JohnnyK

Member
Registered
Joined
Mar 15, 2017
Messages
59
Points
0
Require will throw a PHP Fatal Error if the file cannot be loaded. (Execution stops)
Include produces a Warning if the file cannot be loaded. (Execution continues)
 
Older Threads
Newer Threads
Replies
7
Views
4,284
Replies
4
Views
5,014
Replies
12
Views
4,497
Replies
14
Views
6,134
Latest Threads
Replies
0
Views
123
Replies
1
Views
79

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top