Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

FILTER_VALIDATE_EMAIL

FILTER_VALIDATE_EMAIL selects PHP’s email syntax validator.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

filter_var($address, FILTER_VALIDATE_EMAIL)

FILTER_VALIDATE_EMAIL selects PHP’s email syntax validator. With the default failure convention filter_var returns the accepted string unchanged or false, so test failure strictly. It does not check whether a domain handles mail or whether the mailbox exists. Leading or trailing whitespace is not implicitly trimmed in the example.

foreach (["a+b@example.com", " a@example.com", "broken@"] as $address) {
    $result = filter_var($address, FILTER_VALIDATE_EMAIL);
    echo json_encode([$result, $result === false]), PHP_EOL;
}
["a+b@example.com",false]
[false,true]
[false,true]

Examples

<?php
foreach (["a+b@example.com", " a@example.com", "broken@"] as $address) {
    $result = filter_var($address, FILTER_VALIDATE_EMAIL);
    echo json_encode([$result, $result === false]), PHP_EOL;
}

Result

["a+b@example.com",false]
[false,true]
[false,true]

Sources