How can array_count_values() be used to count the number of empty lines in a file in PHP?

To count the number of empty lines in a file in PHP, you can read the file line by line and use the array_count_values() function to count the occurrences of empty lines. By checking if a line is empty (consisting only of whitespace characters), you can increment a counter for empty lines.

<?php
$file = 'example.txt';
$emptyLinesCount = 0;

$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lineCounts = array_count_values($lines);

if(isset($lineCounts[""])) {
    $emptyLinesCount = $lineCounts[""];
}

echo "Number of empty lines in file: " . $emptyLinesCount;
?>