What are the advantages and disadvantages of using a foreach loop in PHP for processing data from text files?

When processing data from text files in PHP, using a foreach loop can be advantageous as it simplifies the process of iterating through each line of the file. This makes it easier to manipulate and extract data as needed. However, a disadvantage of using a foreach loop is that it may not be the most efficient method for large files, as it loads the entire file into memory.

<?php
// Read data from a text file using a foreach loop
$filename = "data.txt";
$file = fopen($filename, "r");

if ($file) {
    foreach (file($filename) as $line) {
        // Process each line of the file
        echo $line;
    }
    fclose($file);
} else {
    echo "Error opening file.";
}
?>