What are some common mistakes or pitfalls to avoid when integrating PHP code with HTML for dynamic content generation on a website?

One common mistake when integrating PHP code with HTML is not properly escaping user input, which can lead to security vulnerabilities like cross-site scripting (XSS) attacks. To prevent this, always sanitize and validate user input before outputting it on the page.

<?php
$user_input = "<script>alert('XSS attack!');</script>";
$sanitized_input = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');
echo "<p>User input: $sanitized_input</p>";
?>