Do you know the difference on PHP between “include” and “require”?

Introdution

Hello, my name’s Caique and I’m PHP developer. Nice to meet you =D

I love tech and I’m here because something is bitting my mind.

It’s a quite simple question, but a couple of days ago, someone asked me “What is the difference between include and require?”

Simply I’m used to work with it, but I couldn’t answer that question.

I know it’s simple and easy question, but I really want to write that.

Explanation

The include or require are the SAME thing !!!!!!!!!! =O

That statements takes all code that exists in the specified file and copies it into the file that uses the require or include statment.

They are identical, EXCEPT upon FAILUREE:

require will produce a fatal error (E_COMPILE_ERROR) and stop the script.

include will only produce a warning (E_WARNING) and the script will continue.

The conclusion

How to know where and when use one of them? Now I know, hehe =|

If you want to keep executing your application and isn’t necessary at all the included file, and you need to show some output for the user, you will use the include statement.

Otherwise, require statment when are missing, will stop the execution of you application. Are you in a big project? So my advice is, use require, it’s gonna avoid serious issues in your app’s security and integrity when some file is accidentally missing.

OBS: “In case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution.”

Source

I’ve searched on Stackoverflow and throught that wonderful dev paradise I reached on the source that’s is the following:

I hope you enjoied on my first Medium Article.

Thanks folks! See y’all soon.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store