What are the best practices for handling HTML tags and special characters in PHP scripts?

When handling HTML tags and special characters in PHP scripts, it is important to properly sanitize and escape user input to prevent cross-site scripting (XSS) attacks. The htmlspecialchars() function can be used to convert special characters to HTML entities, while strip_tags() can be used to remove any HTML tags from user input.

// Sanitize user input to prevent XSS attacks
$user_input = "<script>alert('XSS attack!')</script>";
$clean_input = htmlspecialchars($user_input, ENT_QUOTES);

echo $clean_input; // Output: <script>alert('XSS attack!')</script>