How can the presence of empty lines at the end of a CSV file impact the functionality of PHP scripts that read from it?

Empty lines at the end of a CSV file can cause PHP scripts to interpret them as empty rows, leading to unexpected behavior or errors when reading the file. To solve this issue, you can modify the PHP script to skip any empty lines at the end of the file before processing the data.

<?php
$filename = 'data.csv';
$file = fopen($filename, 'r');

$data = [];
while (($row = fgetcsv($file)) !== false) {
    if (count($row) > 0) {
        $data[] = $row;
    }
}

fclose($file);

// Process the data array
foreach ($data as $row) {
    // Do something with each row
}
?>