Using Filter_var to Validate Email Addresses in PHP

Comments

As from version 5.2, PHP comes with built-in data filtering. Which makes validating emails and URL very, very easy.

Validating email addresses in PHP

It couldn’t be easier than this, could it?

1
2
3
4
5
6
7
8
9
10
function valid_email( $str=false )    {
  if( !$str ) {
      return false;
  }
  if(filter_var($str, FILTER_VALIDATE_EMAIL)) {
      return true;
  }   else    {
      return false;
  }
}

You can do much more with the new PHP filtering functions, this is all I’ve got time for now.

Comments