How can the code snippet be optimized to avoid the issue with the variable $lineCount?

The issue with the variable $lineCount is that it is being incremented within the loop, which can lead to incorrect counting of lines. To avoid this issue, we can use the count() function to get the total number of lines in the file.

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

// Check if the file exists
if (file_exists($file)) {
    $lines = file($file);
    $lineCount = count($lines);

    echo "Total number of lines in the file: $lineCount";
} else {
    echo "File not found.";
}
?>