w3resource

PHP Exercises: Valid an email address

PHP: Exercise-38 with Solution

Write a PHP program to valid an email address.

Sample Solution: -

PHP Code:

<?php
function valid_email($email)
{
  $result = trim($email);
  if (filter_var($result, FILTER_VALIDATE_EMAIL)) 
  {
    return "true";
  } 
  else 
  {
    echo "false";
  }
}
echo valid_email("[email protected]")."\n";
echo valid_email("abc#example.com")."\n";
?>

Sample Output:

true                                                        
false       

Flowchart:

Flowchart: Valid an email address

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP program to compute the sum of the prime numbers less than 100.
Next: Write a PHP program to get the size of a file.

What is the difficulty level of this exercise?



PHP: Tips of the Day

PHP: PHPDoc type hinting for array of objects?

Use:

/* @var $objs Test[] */
foreach ($objs as $obj) {
    // Typehinting will occur after typing $obj->
}

when typehinting inline variables, and

class A {
    /** @var Test[] */
    private $items;
}

Ref : https://bit.ly/31a2QJN