How can the issue of counting lines in a test file without counting empty lines be addressed in PHP code?

To address the issue of counting lines in a test file without counting empty lines in PHP, we can use the file() function to read the file into an array and then loop through each line to check if it is empty before incrementing a counter. This way, we can accurately count only non-empty lines in the file.

$filename = 'testfile.txt';
$lines = file($filename);
$nonEmptyLineCount = 0;

foreach ($lines as $line) {
    if (trim($line) !== '') {
        $nonEmptyLineCount++;
    }
}

echo "Number of non-empty lines in the file: " . $nonEmptyLineCount;