How can a foreach loop be used to iterate over an array of file names in PHP to check their existence?

To iterate over an array of file names in PHP to check their existence, you can use a foreach loop to loop through each file name and use the file_exists() function to check if the file exists. This way, you can efficiently check the existence of multiple files in the array.

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

foreach ($files as $file) {
    if (file_exists($file)) {
        echo "$file exists.<br>";
    } else {
        echo "$file does not exist.<br>";
    }
}