Skip to content
PHP Logo

How can the script provided be modified to handle cases where the file contains only whitespace characters in PHP?

The issue arises when the file contains only whitespace characters, as the script will consider it empty and not display any content. To solve this, we can modify the script to check if the file contains only whitespace characters and then display a message indicating that the file is empty.

php
// Check if the file contains only whitespace characters
$fileContent = file_get_contents($file);
if (trim($fileContent) === '') {
    echo "File is empty or contains only whitespace characters.";
} else {
    // Display the content of the file
    echo nl2br($fileContent);
}