Home  >  Article  >  Backend Development  >  Learn how to determine whether an email is correct in PHP

Learn how to determine whether an email is correct in PHP

coldplay.xixi
coldplay.xixiforward
2020-07-10 17:26:443006browse

Learn how to determine whether an email is correct in PHP

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;;
}

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 results are as follows:

Then we enter a value at random: rtgdrgdth555.

The judgment results are as follows:

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

filter_var() Function syntax:


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

Parameters:

  • variable: The value to filter.

  • #filter: The ID of the filter to apply.

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

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of Learn how to determine whether an email is correct in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete