Suppose there is a form floating where every user has to fill his/her email ID. It might happen that due to typing error or any other problem user doesn't fill his/her mail ID correctly. Then at that point, the program should be such that it should print a user-friendly message notifying the user that address filled is wrong. This is a simple program and can be done in two ways in PHP language.
Method 1: Naive approach
There is a filter called FILTER_VALIDATE_EMAIL which is in-built in PHP and validates mail ID.
The function filter_var() is also used in this program which takes two arguments. The first is the user mail ID and the second is the email filter. The function will return a Boolean answer according to which we can print the message of our desire.
Program:
Output:
Method 2: Separating strings
How a normal human being validates some email addresses? The human observes some pattern in the string as well as some special characters while checking the validation of an email. The same can be done through programming. An email ID should necessarily have the character '@' and a string '.com' in a specific order. A function called preg_match() will be used for checking this order and characters.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer