How can the PHP script be modified to ensure that all comments for a tutorial are displayed correctly?

The issue of not displaying comments correctly for a tutorial in a PHP script can be solved by ensuring that the comment text is properly escaped before being displayed on the webpage. This can be done using the htmlspecialchars() function in PHP to convert special characters to HTML entities, preventing any potential security vulnerabilities.

<?php
// Retrieve comments from database
$comments = // Retrieve comments from database query

// Display comments
foreach($comments as $comment){
    echo "<div>" . htmlspecialchars($comment['comment_text']) . "</div>";
}
?>