How can the code be optimized using foreach and glob functions in PHP?

The code can be optimized by using the `glob` function to retrieve an array of files matching a specified pattern, and then using a `foreach` loop to iterate over each file in the array. This approach eliminates the need for manually listing each file in the directory, making the code more scalable and maintainable.

$files = glob("path/to/directory/*.txt");

foreach ($files as $file) {
    echo "Processing file: $file\n";
    // Add your processing logic here
}