What are the potential risks of not validating user input in PHP?

Not validating user input 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 validate and sanitize user input before using it in your application.

// Example of validating user input in PHP
$username = $_POST['username'];

// Validate username input
if (preg_match("/^[a-zA-Z0-9]*$/", $username)) {
    // Valid username, proceed with using it in your application
} else {
    // Invalid username, handle the error accordingly
}