What are the potential security risks associated with user input in PHP code and how can they be mitigated?

Potential security risks associated with user input in PHP code include SQL injection, cross-site scripting (XSS), and remote code execution. To mitigate these risks, it is important to sanitize and validate user input before using it in your code. This can be done by using functions like htmlentities() to prevent XSS attacks, prepared statements to prevent SQL injection, and limiting the execution of user input to prevent remote code execution.

// Example of sanitizing user input to prevent XSS attacks
$user_input = "<script>alert('XSS attack!');</script>";
$sanitized_input = htmlentities($user_input);
echo $sanitized_input;