What is the best way to count the number of lines in a CSV file using PHP?

To count the number of lines in a CSV file using PHP, you can read the file line by line and increment a counter for each line. This can be achieved by opening the CSV file, looping through each line using a while loop, and incrementing a counter variable for each iteration. Finally, you can output the total count of lines in the CSV file.

<?php

$filename = 'example.csv';
$lineCount = 0;

$handle = fopen($filename, 'r');
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        $lineCount++;
    }

    fclose($handle);
}

echo "Total number of lines in $filename: $lineCount";
?>