I'm writing a PHP application for the first time (other than toys and exercises), and I'm at a loss to understand why PHP includes both an include
and a require
construct.
Before you write an answer explaining the differences between the two, let me first say that I do understand the difference - include
produces a warning and keeps on going, and require
produces a fatal error. My question is: when would you want to include, but not require a file?
Maybe it's a failure of imagination on my part, but there don't seem to be any files in my application that I don't want to scream about if they're not there. Oddly, this doesn't make me want to use require
since it seems impossible to properly handle a failed require
, so instead I use a helper function along the lines of this (warning: air code):
public static function include($filename) {
if (is_readable($filename)) {
if (!@include($filename)) {
throw new FileNotFoundException("File deleted after readable check");
}
} else {
throw new FileNotFoundException("File missing or unreadable");
}
}
I guess what I'm asking is:
The require() function will stop the execution of the script when an error occurs. The include() function does not give a fatal error. The include() function is mostly used when the file is not required and the application should continue to execute its process when the file is not found.
The difference is that the include() function produces a warning, but the script will continue execution, while the require() function produces a warning and a fatal error i.e. the script will not continue execution.
The include() function is used to include a PHP file into another irrespective of whether the file is included before or not. The include_once() will first check whether a file is already included or not and if it is already included then it will not include it again.
The include_once keyword is used to embed PHP code from another file. If the file is not found, a warning is shown and the program continues to run. If the file was already included previously, this statement will not include it again.
I'd use require
for loading files essential for the app itself, i.e. require 'database_config.php'
, require 'core.php'
. If this fails, you want it to fail as fast, hard and merciless as possible, since something is obviously wrong with your app installation. In this state it's not even guaranteed that it could handle a thrown exception properly, and require
produces a very clear error message without any extra code.
include
should be used for things like template files that you want to use when the app is already up and running and can handle its own errors gracefully.
Example:
include 'error_handler.php';
set_error_handler('error_handler');
/* something bad happens */
Warning: 'error_handler.php' not found!
Error: Specified error handler "error_handler" doesn't exist.
At some point you simply rely on basic files, even if it's just your error handler. You'd have to introduce extra code to handle a missing error handler gracefully, and even then the best you could do is output some error and die
(unless you want to get into the game of catching errors gracefully even if your error handler doesn't exist). It's best to simply require 'error_handler.php'
; you can include
and custom handle everything after this if you want to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With