What are the potential pitfalls to avoid when outputting PHP strings in HTML?

One potential pitfall to avoid when outputting PHP strings in HTML is not properly escaping the strings, which can lead to security vulnerabilities such as cross-site scripting (XSS) attacks. To solve this issue, always use htmlspecialchars() function to escape the strings before outputting them in HTML.

<?php
// Outputting a PHP string in HTML with proper escaping
$string = "<script>alert('XSS attack!');</script>";
echo htmlspecialchars($string);
?>