Web Development

This part is of the PHP Post Series dedicated to spread knowledge about PHP

Code organization is one of the reasons for including and requiring files in PHP applications. This is mostly used for configuration files, settings, etc.

Include

include() is a function that allows you to include files in PHP files.

Example Code
include('simplepie.inc');

Include Once

include_once makes sure that a file is included only once in the file. If the file was already included, the file won’t be included again.

include_once('simplepie.inc');
Include / Include Once Trick

include and include_once can also be written as follows if we want to check if a file has been included.

if (@include('simplie.inc')) {
    echo 'File has been included';
} else {
    echo 'File has not been included';
}

Require

Another function to be used when including files is the require function. The require function doesn’t just include a file but requires it. This means that if a file fails to be included, probably due to the file not existing, a fatal error is thrown.

require('simplepie.inc');

Require Once

require_once just like include_once makes sure that a file is included only once.

require_once('simplepie.inc');