How can the PHP code be modified to ensure that only valid, non-empty lines from the comments.txt file are displayed in the output?
To ensure that only valid, non-empty lines from the comments.txt file are displayed in the output, we can modify the PHP code to check for empty lines before displaying them. This can be done by using the `trim()` function to remove any whitespace from the beginning and end of each line, and then checking if the resulting string is not empty before displaying it.
<?php
$filename = 'comments.txt';
$handle = fopen($filename, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$trimmedLine = trim($line);
if (!empty($trimmedLine)) {
echo $trimmedLine . "<br>";
}
}
fclose($handle);
} else {
echo "Error opening the file.";
}
?>