How to determine if the email is correct in php

藏色散人
Release: 2023-04-04 13:52:01
Original
5689 people have browsed it

php method to determine whether the email is correct: first create a PHP sample file; then enter an email that needs to be judged; finally pass "if (filter_var($email, FILTER_VALIDATE_EMAIL)){...}" Just use the method to judge.

How to determine if the email is correct in php

The operating environment of this article: Windows 7 system, Dell G3 computer, PHP version 7.1.

PHP Determining whether the email is correct or valid is one of the common questions in our PHP interview process. We can use PHP filters to achieve judgment.

Below we will use specific examples to introduce to you a simple implementation method of PHP to determine whether the email address is correct.

The code is as follows:

<?php

$email = "demo@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
    echo &#39;"&#39; . $email . &#39;" 是有效的。&#39;;
}
else
{
    echo &#39;"&#39; . $email . &#39;" 是无效的。&#39;;
}
Copy after login

We use the FILTER_VALIDATE_EMAIL filter to verify the email address. Here we need to determine whether the email demo@example.com is valid.

The front desk access result is as follows:

How to determine if the email is correct in php

Then we enter a value at random: rtgdrgdth555.

The judgment results are as follows:

How to determine if the email is correct in php

The filter_var() function uses the specified filter to filter variables.

filter_var() function syntax:

mixed filter_var(mixed $ variable [,int $ filter = FILTER_DEFAULT [,mixed $ options]])
Copy after login

Parameters:

variable: the value to be filtered.

filter: The ID of the filter to apply.

options: An associative array of options, or a bitwise identifier.

This article is an introduction to the simple method of PHP to determine whether the email is correct. I hope it will be helpful to friends in need!

The above is the detailed content of How to determine if the email is correct in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template