In addition to affecting layout, how can user-input HTML code in PHP also pose security risks, especially when combined with JavaScript?

User-input HTML code in PHP can pose security risks when combined with JavaScript because it can allow for Cross-Site Scripting (XSS) attacks. This occurs when a user inputs malicious code that is then executed on other users' browsers, potentially leading to data theft or website defacement. To prevent this, it is important to sanitize and validate user input before displaying it on a webpage.

// Sanitize and validate user input
$userInput = htmlspecialchars($_POST['user_input']);
echo $userInput;