What is the potential issue with dynamically inserting a phrase into HTML text using PHP, and how can it affect the HTML syntax?

When dynamically inserting a phrase into HTML text using PHP, the potential issue is that the inserted phrase may contain characters that could break the HTML syntax, leading to rendering issues or even security vulnerabilities. To solve this problem, you should properly escape the inserted phrase to ensure that any special characters are converted into their respective HTML entities.

<?php
$phrase = "<script>alert('Hello!');</script>";
$escaped_phrase = htmlspecialchars($phrase, ENT_QUOTES, 'UTF-8');
echo "<p>" . $escaped_phrase . "</p>";
?>