What is the significance of using the file_exists function in PHP when loading files in a loop?

When loading files in a loop in PHP, it is important to check if the file exists before trying to load it to avoid errors. Using the file_exists function allows you to verify the existence of a file before attempting to include or require it in your script. This helps prevent issues such as "file not found" errors and ensures that your script runs smoothly without any interruptions.

$files = ['file1.php', 'file2.php', 'file3.php'];

foreach ($files as $file) {
    if (file_exists($file)) {
        include $file;
    } else {
        echo "File $file does not exist.";
    }
}