In this article, we will look at the difference between include and require in PHP.
As you already know, both of these language statements carry out a similar action. Both include and require are used to “incorporate” another PHP file into the script that is making the call.
However, there is a pretty big difference between the two.
This difference will only become apparent if the path to your file is incorrect or the file in question does not exist.
When the include statement cannot find a file.
If the include statement cannot find the file in question, it will emit a warning message like so:
Warning: include(file.php): failed to open stream: No such file or directory in /path/to/script.php on line 4
It will also emit a second E_WARNING message that says include() could not open the file for inclusion:
Warning: include(): Failed opening ‘file.php’ for inclusion
However, despite this warning, the rest of your script will continue to execute. i.e. The script will not be halted.
When the require statement cannot find a file.
The require statement is different than include because it must find the file. Otherwise, it will kill your script with a fatal error:
Fatal error: require(): Failed opening required ‘file.php’
This means that any code that occurs afterwards will not be executed.
Take the following example:
//Print the word hello echo 'Hello!'; //Attempt to require PHP file called database.php require 'database.php'; //Attempt to call a function called database_connection $db = database_connection();
If you run the code above, you will see the code prints out “Hello!”
However, the script will experience a fatal error when it attempts to require database.php. As a result, the code after our failed require statement is not executed and no call to database_connection() is ever made.
Should I use include or require?
Some PHP developers will advise you to use include if the file in question is not critical to the functioning of the application.
Example: “If it’s just HTML, use include. If it’s something as critical as a database connection, then use require.”
However, I am personally uneasy with such advice.
In my opinion, the file should always exist. If it doesn’t exist, then there is a big problem.
I have yet to come across a valid reason for attempting to include a file that might not exist. If you are attempting to include the file, then I would assume that the file is necessary in some way or another.
Otherwise, why would you try to include it?
I have even seen tutorials where developers use the @ operator to suppress warnings from a failed include statement:
//Suppressing the warning message @include 'database.php';
That is just madness.
Make no mistake about it, this is horrible advice.
Using the @ operator in such a fashion is dangerous, as it can result in easily-fixable errors going undetected for weeks.
Error suppression can also make debugging an absolute nightmare.
Using the @ operator on an include statement is even worse, as it will actually suppress any errors that occur inside the file.
In other words, if the file exists and the include statement is successful in “importing it”, any warning errors that exist inside that file will be suppressed. That is bad news.
Finally: It is also worth noting that there is no real measurable performance difference between require and include. Simply put, worrying about that kind of “difference” is a complete waste of time.