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

Gregorio

New member
Joined
Dec 3, 2012
Messages
15
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

New member
Joined
Jan 6, 2016
Messages
121
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

New member
Joined
Jan 12, 2016
Messages
36
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

New member
Joined
Jul 17, 2015
Messages
28
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

New member
Joined
Jan 24, 2016
Messages
467
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.
 
Newer threads
Replies
11
Views
4,580
Replies
4
Views
2,892
Replies
12
Views
3,433
Replies
14
Views
3,895
Latest threads
Replies
1
Views
34
Replies
1
Views
195
Replies
0
Views
205
Replies
0
Views
238
Replies
5
Views
555
Recommended threads
Replies
28
Views
7,307
Replies
6
Views
1,626
Replies
4
Views
2,891
Replies
6
Views
3,123
Replies
0
Views
3,965

Referral contests

Referral link for :

Sponsors

Popular tags

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