The difference between include and require in PHP.

This is a short article about the difference between include and require in PHP. As you already know, both of these are 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 noticeable if the path to your file is incorrect or the PHP 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:

If you run the code above, you will see that “Hello!” is printed out. However, the script will be halted by 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 that you can 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’m personally uneasy with such advice. In my opinion, the file should always exist and if it doesn’t exist, then there is a big problem that needs to be fixed. I have yet to come across a valid reason for attempting to include a file that might not 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 recommend using the @-operator to suppress warnings from a failed include statement:

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. i.e. 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.

Hopefully, you found this guide to be informative!

Facebook Comments