Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Wrapping Up The Project Using A Third-Party Library

what the point from require_once and include)once?

hi i'm just wondering , can't we just add the include or the require only 1 time in code then it's not gonna load again ?!

thank you

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Ahmed:

In general the usage is as follows:

Use

require when the file is required by your application, e.g. an important message template or a file containing configuration variables which with without the app would break.

require_once when the file contains content that would produce an error on subsequent inclusion, e.g. function important() { /* important code */} is definitely needed in your application but since functions cannot be redeclared should not be included again.

include when the file is not required and application flow should continue when not found, e.g. great for templates referencing variables from the current scope or something

include_once optional dependencies that would produce errors on subsequent loading or maybe remote file inclusion that you do not want to happen twice due to the HTTP overhead

The difference between require and include is that the require() function handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

Post back if you have additional questions.

Happy coding,

Ken