How can the use of preg_match() in PHP for form validation lead to potential errors or security risks?

Using preg_match() for form validation can lead to potential errors or security risks if the regular expression used is not properly constructed. This can result in false positives or negatives, allowing malicious input to pass through or valid input to be rejected. To mitigate this risk, it's important to carefully design and test the regular expression to ensure it accurately validates the input as intended.

// Example of using filter_var() for form validation instead of preg_match()

$email = $_POST['email'];

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Valid email address
} else {
    // Invalid email address
}