DEV Community

Cover image for Difference between include, require, include_once and require_once in PHP
Amine Saissi Hassani
Amine Saissi Hassani

Posted on

Difference between include, require, include_once and require_once in PHP

First of all we should know what is the difference and similarity between include() and require(), include() and require() are used to include other files into a PHP file, and the difference between them are the include() generates a warning when there is an error, but the script will continue execution, otherwise, the require() generates a fatal error, and the script will stop.

The require_once() statement is similar to require() except require_once() will check if the file has already been included, and if so, it will not require it again, same with include_once().

Top comments (15)

Collapse
 
samuelroland profile image
Samuel Roland

It's worth mentioning that the error you are talking about is that the file that we try to import is not found. include() can cause crash too for other reasons (the included php script contains functions already declared at the last inclusion for ex -> for this reason we use *_once() options).

Collapse
 
aminesaissihassani profile image
Amine Saissi Hassani

oh thank you for your feedback I really appreciate it, I will edit this post and add more examples and explanation!
Thank you!

Collapse
 
samuelroland profile image
Samuel Roland

But thanks for this little explanation !

Collapse
 
abdelhalimlakfifi profile image
abdelhalim LAKFIFI

Thank you that's amazing

Collapse
 
aminesaissihassani profile image
Amine Saissi Hassani

Thank you!

Collapse
 
aabiseverywhere profile image
Aabishkar Wagle

Thanks, very short nice and to the point.

Collapse
 
aminesaissihassani profile image
Amine Saissi Hassani

Thank you!

Collapse
 
yoshida profile image
Masao Yoshida

Your introduction was short and clear.
Thanks.

Collapse
 
aminesaissihassani profile image
Amine Saissi Hassani

Thank you!

Collapse
 
nnafzaoui profile image
nnafzaoui

Nice, thanks keep writing

Collapse
 
aminesaissihassani profile image
Amine Saissi Hassani • Edited

Thank you, I'll keep writing!

Collapse
 
soumiakab profile image
KABRANE SOUMIA

good

Collapse
 
aminesaissihassani profile image
Amine Saissi Hassani

Thanks!

Collapse
 
wporta06 profile image
wporta06

This is really helpful, thank you!

Collapse
 
aminesaissihassani profile image
Amine Saissi Hassani

Thank you!