include, include_once, require or require_once?

If your page will not work without the DB connection, then require_once would be the only correct option (since you don't want to load these settings twice, loading them once should suffice). Include will try to load your page even if the settings file is not available.


Functional Work : All functions perform similar work. All functions will include and evaluates the specific file while executing the code.

Functional Difference :

include vs include_once : There is only one difference between include() and include_once(). If the code from a file has been already included then it will not be included again if we use include_once(). Means include_once() include the file only once at a time.

include vs require : if include() is not able to find a specified file on location at that time it will throw a warning however, it will not stop script execution. For the same scenario, require() will throw a fatal error and it will stop the script execution.

require vs require_once : There is only one difference between require() and require_once(). If the code from a file has been already included then it will not be included again if we use require_once(). Means require_once() include the file only once at a time.

To get the detailed knowledge with example please review these amazing articles
(1) http://www.readmyviews.com/include-vs-include-once/
(2) http://www.readmyviews.com/include-vs-require/


The only difference between the two is that require and its sister require_once throw a fatal error if the file is not found, whereas include and include_once only show a warning and continue to load the rest of the page. If you don't want PHP to attempt to load the rest of your page without the database info (which I would assume), then use require_once. You don't need to include the file more than once, so there is no need to use the regular require function.


For the database connection variables, use of require_once() function will be preferable. If the connection fails for any reason you can show the failure message.

Tags:

Php

Include