Is there any difference between them? Is using them a matter of preference? Does using one over the other produce any advantages? Which is better for security?
- 4possible duplicate of When should I use require_once vs include?Gordon– Gordon2010-09-03 07:56:32 +00:00Commented Sep 3, 2010 at 7:56
- 3Always use "require". "include" is as convenient as an electric door in a sauna.Marco Mariani– Marco Mariani2010-09-03 09:50:48 +00:00Commented Sep 3, 2010 at 9:50
- 2@MarcoMariani How would that be inconvenient? It's probably clear, I'm just not seeing it. Perhaps the steam?Austin Burk– Austin Burk2014-06-12 19:44:12 +00:00Commented Jun 12, 2014 at 19:44
- To put it simply, if a 'foo.php' file is missing by mistake, I want to know as soon as possible, not when a function that should have been in foo.php is called. Replacing include with require can often reveal bugs. Let's say config.php is missing, and the application is running with a default configuration. Which is better for security? As for the sauna, when I'm inside and the door does not open for some reason I don't like it.Marco Mariani– Marco Mariani2014-06-13 07:55:05 +00:00Commented Jun 13, 2014 at 7:55
7 Answers
require will throw a PHP Fatal Error if the file cannot be loaded. (Execution stops)
include produces a Warning if the file cannot be loaded. (Execution continues)
Here is a nice illustration of include and require difference:
From: Difference require vs. include php (by Robert; Nov 2012)
2 Comments
You find the differences explained in the detailed PHP manual on the page of require:
requireis identical toincludeexcept upon failure it will also produce a fatalE_COMPILE_ERRORlevel error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.
See @efritz's answer for an example
5 Comments
<?php if (isset($flibbertygibbet)) require 'file.php'; would seem to make this answer look totally incorrect. Otherwise, i should get a fatal error even though the condition isn't true. strace doesn't show PHP even trying to touch file.php.if (false) require 'file.php'; would cause 'file.php' to be loaded (but not executed). TLDR disregard all of these comments.Use include if you don't mind your script continuing without loading the file (in case it doesn't exist etc) and you can (although you shouldn't) live with a Warning error message being displayed.
Using require means your script will halt if it can't load the specified file, and throw a Fatal error.
Comments
The difference between include() and require() arises when the file being included cannot be found: include() will release a warning (E_WARNING) and the script will continue, whereas require() will release a fatal error (E_COMPILE_ERROR) and terminate the script. If the file being included is critical to the rest of the script running correctly then you need to use require().
For more details : Difference between Include and Require in PHP
Comments
As others pointed out, the only difference is that require throws a fatal error, and include - a catchable warning. As for which one to use, my advice is to stick to include. Why? because you can catch a warning and produce a meaningful feedback to end users. Consider
// Example 1. // users see a standard php error message or a blank screen // depending on your display_errors setting require 'not_there'; // Example 2. // users see a meaningful error message try { include 'not_there'; } catch(Exception $e) { echo "something strange happened!"; } NB: for example 2 to work you need to install an errors-to-exceptions handler, as described here http://www.php.net/manual/en/class.errorexception.php
function exception_error_handler($errno, $errstr, $errfile, $errline ) { throw new ErrorException($errstr, 0, $errno, $errfile, $errline); } set_error_handler("exception_error_handler"); 1 Comment
<?PHP echo "Firstline"; include('classes/connection.php'); echo "I will run if include but not on Require"; ?> A very simple Practical example with code. The first echo will be displayed. No matter you use include or require because its runs before include or required.
To check the result, In second line of a code intentionally provide the wrong path to the file or make error in file name. Thus the second echo to be displayed or not will be totally dependent on whether you use require or include.
If you use require the second echo will not execute but if you use include not matter what error comes you will see the result of second echo too.
Comments
In case of Include Program will not terminate and display warning on browser,On the other hand Require program will terminate and display fatal error in case of file not found.
