What are the implications of the client-side interpretation of HTML generated by PHP scripts?

Client-side interpretation of HTML generated by PHP scripts can lead to security vulnerabilities such as cross-site scripting (XSS) attacks if user input is not properly sanitized. To prevent this, it is crucial to escape any user input before outputting it in HTML to ensure that it is treated as plain text and not as executable code by the browser.

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