Write a simple PHP program to check that emails are valid
Use FILTER_VALIDATE_EMAIL filter that validates value as an e-mail address.
The FILTER_VALIDATE_EMAIL filter validates an e-mail address.
filter_var()
filter_var — Filters a variable with a specified filter
syntax:-
filter_var(mixed $value, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed
Parameters
value
Value to filter. Note that scalar values are converted to string internally before they are filtered.
filter
The ID of the filter to apply. The Types of filters manual page lists the available filters.
If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering taking place by default
options
Associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in "flags" field of array. For the "callback" filter, callable type should be passed. The callback must accept one argument, the value to be filtered, and return the value after filtering/sanitizing it.
code
<?php
$email = "mail@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo "Valid";
}
else
{
echo "Invalid";
}
?>
output:-
valid
Post a Comment
If you have any doubts, Please let me know
Thanks!