Is it more efficient to search through each file individually or use a different method?

When searching through multiple files, it is more efficient to use a different method such as iterating through the files in a directory and searching for the desired content within each file. This approach avoids the need to open and read each file individually, which can be time-consuming and resource-intensive.

$directory = '/path/to/directory';
$searchTerm = 'keyword';

foreach (new DirectoryIterator($directory) as $fileInfo) {
    if ($fileInfo->isFile()) {
        $content = file_get_contents($fileInfo->getPathname());
        if (strpos($content, $searchTerm) !== false) {
            echo "Found in file: " . $fileInfo->getFilename() . "\n";
        }
    }
}