Are there any potential pitfalls to avoid when combining PHP and HTML code?

One potential pitfall to avoid when combining PHP and HTML code is forgetting to properly escape PHP variables to prevent cross-site scripting attacks. To solve this issue, always use htmlspecialchars() or htmlentities() functions to sanitize any user input before outputting it in HTML.

<?php
$name = "<script>alert('XSS attack!')</script>";
echo htmlspecialchars($name);
?>