What are the best practices for reading and processing text files line by line in PHP?

When reading and processing text files line by line in PHP, it is important to use efficient methods to avoid memory issues with large files. One common approach is to use a loop to read the file line by line using functions like fgets() or fgetcsv(). It is also recommended to close the file handle after processing to free up resources.

// Open the file for reading
$handle = fopen("file.txt", "r");

// Loop through the file line by line
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // Process the line here
        echo $line;
    }
    // Close the file handle
    fclose($handle);
} else {
    echo "Error opening the file.";
}