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,945
Replies
4
Views
3,140
Replies
12
Views
3,754
Replies
14
Views
4,313
Latest threads
Top