What are the potential pitfalls of not properly validating characters in a PHP string?

Not properly validating characters in a PHP string can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To prevent these risks, it is important to sanitize user input by validating and filtering out any potentially harmful characters before using the string in database queries or outputting it to the browser.

// Example of properly validating characters in a PHP string
$user_input = $_POST['user_input'];
$clean_input = filter_var($user_input, FILTER_SANITIZE_STRING);
// Use $clean_input in your code instead of $user_input