What are the potential risks of not sanitizing user inputs in PHP, and how can they be mitigated?

Not sanitizing user inputs in PHP can lead to security vulnerabilities such as SQL injection, cross-site scripting (XSS), and other forms of attacks. To mitigate these risks, it is important to sanitize user inputs by using functions like htmlspecialchars() to prevent XSS attacks and prepared statements to prevent SQL injection.

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

// Example of sanitizing user input using prepared statements to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$user_input]);