What are the potential pitfalls of using include for HTML code in PHP, and what alternative methods could be used?

Using include for HTML code in PHP can lead to security vulnerabilities such as code injection or XSS attacks if the included file is not properly sanitized. To mitigate these risks, it is recommended to use PHP's htmlspecialchars function to escape any user input before including it in the HTML code.

<?php
// Example of using htmlspecialchars to escape user input before including it in HTML code
$user_input = "<script>alert('XSS attack!');</script>";
$escaped_input = htmlspecialchars($user_input);
echo "<div>$escaped_input</div>";
?>