Interview Questions,  Software Development

Difference between require, include, require_once and include_once

require vs include

The require() function is identical to include(), except that it 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.

require_one vs include_one

The require() function is identical to include(), except that it 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.

Using

require when the file is required by your application, e.g. an important message template or a file containing configuration variables without which 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 re-declared 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_onceoptional 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