What best practices should be followed when including external files in PHP scripts?

When including external files in PHP scripts, it is important to follow best practices to ensure security and maintainability. One key practice is to use absolute paths instead of relative paths to include files, as this helps prevent directory traversal attacks. Additionally, it is recommended to sanitize any user input used in file includes to prevent code injection vulnerabilities. Lastly, consider using functions like require_once or include_once to avoid including the same file multiple times.

// Example of including an external file using absolute path and sanitizing user input
$filename = '/path/to/external/file.php';
if (file_exists($filename)) {
    include($filename);
} else {
    echo 'File not found.';
}