How does the preg_match function work in PHP and what is its role in the code provided?
The preg_match function in PHP is used to perform a regular expression match on a string. It searches for a pattern in the given string and returns true if a match is found, otherwise it returns false. In the provided code, the preg_match function is used to check if the email address entered by the user is in a valid format.
$email = "example@example.com";
if (preg_match("/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/", $email)) {
echo "Valid email address";
} else {
echo "Invalid email address";
}
Related questions
-
escape characters functionality PHP code
-
PHP image manipulation transparency
-
PHP sessions form data session variables