What is the purpose of checking if a file exists before including it in PHP?

Checking if a file exists before including it in PHP is important to prevent errors or security vulnerabilities. If the file does not exist, including it will result in a warning or fatal error, disrupting the execution of the script. By checking if the file exists before including it, we can ensure that the script runs smoothly and securely.

$file = 'path/to/file.php';

if (file_exists($file)) {
    include $file;
} else {
    echo 'File does not exist.';
}