In PHP, include and require are two common language constructs used for including external files into a PHP script. They are used to modularize code and reuse it across different PHP files, making code maintenance and organization easier. While both include and require achieve similar functionality, there are subtle differences between them.

Include and Require in PHP

#1 Error handling

One of the main differences between include and require is how they handle errors. When using “include” and the included file is not found, a warning is generated, and the script continues to execute the remaining code. On the other hand, if “require” is used and the required file is not found, a fatal error is generated, and the script terminates immediately.

Example:

// Using include
include 'header.php'; // If header.php is not found, a warning is generated
echo "Welcome to my website"; // This line will be executed

// Using require
require 'footer.php'; // If footer.php is not found, a fatal error is generated
echo "Thank you for visiting"; // This line will not be executed

#2 Inclusion behavior

Another difference between include and require is how they handle multiple inclusions of the same file. When using “include”, if the same file is included multiple times, it will be included only once, and subsequent inclusions will be ignored. On the other hand, when using “require”, if the same file is required multiple times, it will result in a fatal error.

Example:

// Using include
include 'header.php'; // This file will be included
include 'header.php'; // This file will be ignored

// Using require
require 'footer.php'; // This file will be required
require 'footer.php'; // A fatal error will be generated

#3 Error severity

The severity of errors generated by include and require is also different. As mentioned earlier, include generates a warning when a file is not found, whereas require generates a fatal error. This means that warnings generated by include do not stop the script from executing, while fatal errors generated by require terminate the script immediately.

Example:

// Using include
include 'header.php'; // If header.php is not found, a warning is generated but the script continues to execute
echo "Welcome to my website"; // This line will be executed

// Using require
require 'footer.php'; // If footer.php is not found, a fatal error is generated and the script terminates
echo "Thank you for visiting"; // This line will not be executed

#4 Return value

Include and require also differ in their return value. When using “include”, if the included file is not found, it returns “false”. On the other hand, when using “require”, if the required file is not found, it also returns “false”. However, in case of a successful inclusion or requirement, both include and require return “true”.

Example:

// Using include
$result = include 'header.php'; // If header.php is found, $result will be true, else false

// Using require
$result = require 'footer.php'; // If footer.php is found, $result will be true, else false

#5 Usage scenarios

Both include and require have their use cases based on different scenarios. “Include” is generally used when you want to include a file that is not crucial to the script’s functionality and can be skipped if not found, such as including a template or a configuration file. “Require” is used when you want to include a file that is crucial to the script’s functionality, and the script should not continue if the file is not found, such as including a library or a database connection file.

Example:

// Using include for non-crucial file
include 'config.php'; // If config.php is not found, a warning is generated, but the script continues to execute

// Using require for crucial file
require 'database.php'; // If database.php is not found, a fatal error is generated, and the script terminates

Also Read:

Conclusion:

In conclusion, while include and require in PHP are used for similar purposes of including external files into a PHP script, they differ in error handling, inclusion behavior, error severity, return value, and appropriate usage scenarios. Understanding these differences is important to choose the right one for your specific needs.

It’s worth noting that PHP also provides “include_once” and “require_once” constructs, which are similar to include and require, respectively, but they ensure that the same file is included only once, even if it is called multiple times in the same script.

In summary, use “include” when including a file that is not crucial to the script’s functionality and can be skipped if not found, and use “require” when including a file that is crucial to the script’s functionality and should not be skipped. Also, be mindful of the error handling, inclusion behavior, error severity, and return value differences between include and require while using them in your PHP projects.

I hope this article helps clarify the differences between include and require in PHP and assists you in making informed decisions when using them in your PHP code.

Happy coding!

Categorized in: