What are the best practices for preventing HTML code from being executed in PHP scripts?

To prevent HTML code from being executed in PHP scripts, it is important to properly sanitize user input and escape any output that may contain HTML code. This can help prevent cross-site scripting (XSS) attacks and ensure that the HTML code is treated as plain text rather than being rendered as actual HTML.

// Sanitize user input
$input = "<script>alert('XSS attack');</script>";
$sanitized_input = htmlspecialchars($input, ENT_QUOTES);

// Output the sanitized input
echo $sanitized_input;