What is the best way to handle errors when using the include() function in PHP?

When using the include() function in PHP, it is important to handle errors properly to prevent potential security vulnerabilities or unexpected behavior in your application. One way to handle errors is to use the require() function instead, which will cause a fatal error if the file cannot be included, ensuring that the script does not continue execution. Additionally, you can use the file_exists() function to check if the file exists before including it, and use error handling mechanisms such as try-catch blocks to catch and handle any exceptions that may occur during the include process.

// Example of handling errors when using include() function
if (file_exists('file-to-include.php')) {
    require('file-to-include.php');
} else {
    echo 'Error: File not found';
}