wppaste
WordPress

is_email( string $email, bool $deprecated = false ): string|false

Since
0.71
Source
wp-includes/formatting.php:3543
Verifies that an email is valid.

Description

Does not grok i18n domains. Not RFC compliant.

Compatibility

WordPress
since 0.71
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$emailstring
Email address to verify.
$deprecatedbooloptional
Deprecated.Default: false

Return value

string|false
Valid email address on success, false on failure.

Performance profile

How much work a call to is_email() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
14–53

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 111.

Plugin surface
1 hook

Third-party callbacks on 'is_email' run inside this call, and their cost is not bounded by anything here.

Called by
25

25 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost is_email() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($deprecated)14–18apply_filters()
!empty($deprecated)18–22_deprecated_argument(), apply_filters()
empty($deprecated)28–33explode(), apply_filters()
!empty($deprecated)32–37_deprecated_argument(), explode(), apply_filters()
empty($deprecated) && $local && !$domain && $domain === false41–49explode(), explode(), apply_filters()
!empty($deprecated) && $local && !$domain && $domain === false45–53_deprecated_argument(), explode(), explode(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11114–5311
8.511114–5311
8.411114–531118 fewer instructions than PHP 8.3
8.312914–7111
8.212914–7111
8.112914–7111
7.412914–7111

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 9

9 hooks fire while is_email() runs, in this order:

  1. apply_filters( is_email )filterline 3563 (+20 into the body)

    Filters whether an email address is valid.

  2. apply_filters( is_email )filterline 3569 (+26 into the body)

    Filters whether an email address is valid.

  3. apply_filters( is_email )filterline 3581 (+38 into the body)

    Filters whether an email address is valid.

  4. apply_filters( is_email )filterline 3590 (+47 into the body)

    Filters whether an email address is valid.

  5. apply_filters( is_email )filterline 3596 (+53 into the body)

    Filters whether an email address is valid.

  6. apply_filters( is_email )filterline 3605 (+62 into the body)

    Filters whether an email address is valid.

  7. apply_filters( is_email )filterline 3613 (+70 into the body)

    Filters whether an email address is valid.

  8. apply_filters( is_email )filterline 3619 (+76 into the body)

    Filters whether an email address is valid.

  9. apply_filters( is_email )filterline 3625 (+82 into the body)

    Filters whether an email address is valid.

Uses · 2

Used by · 25

Show all 25

Source code

function is_email( $email, $deprecated = false ) {	if ( ! empty( $deprecated ) ) {		_deprecated_argument( __FUNCTION__, '3.0.0' );	} 	// Test for the minimum length the email can be.	if ( strlen( $email ) < 6 ) {		/**		 * Filters whether an email address is valid.		 *		 * This filter is evaluated under several different contexts, such as 'email_too_short',		 * 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',		 * 'domain_no_periods', 'sub_hyphen_limits', 'sub_invalid_chars', or no specific context.		 *		 * @since 2.8.0		 *		 * @param string|false $is_email The email address if successfully passed the is_email() checks, false otherwise.		 * @param string       $email    The email address being checked.		 * @param string       $context  Context under which the email was tested.		 */		return apply_filters( 'is_email', false, $email, 'email_too_short' );	} 	// Test for an @ character after the first position.	if ( strpos( $email, '@', 1 ) === false ) {		/** This filter is documented in wp-includes/formatting.php */		return apply_filters( 'is_email', false, $email, 'email_no_at' );	} 	// Split out the local and domain parts.	list( $local, $domain ) = explode( '@', $email, 2 ); 	/*	 * LOCAL PART	 * Test for invalid characters.	 */	if ( ! preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {		/** This filter is documented in wp-includes/formatting.php */		return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );	} 	/*	 * DOMAIN PART	 * Test for sequences of periods.	 */	if ( preg_match( '/\.{2,}/', $domain ) ) {		/** This filter is documented in wp-includes/formatting.php */		return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );	} 	// Test for leading and trailing periods and whitespace.	if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) {		/** This filter is documented in wp-includes/formatting.php */		return apply_filters( 'is_email', false, $email, 'domain_period_limits' );	} 	// Split the domain into subs.	$subs = explode( '.', $domain ); 	// Assume the domain will have at least two subs.	if ( 2 > count( $subs ) ) {		/** This filter is documented in wp-includes/formatting.php */		return apply_filters( 'is_email', false, $email, 'domain_no_periods' );	} 	// Loop through each sub.	foreach ( $subs as $sub ) {		// Test for leading and trailing hyphens and whitespace.		if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) {			/** This filter is documented in wp-includes/formatting.php */			return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );		} 		// Test for invalid characters.		if ( ! preg_match( '/^[a-z0-9-]+$/i', $sub ) ) {			/** This filter is documented in wp-includes/formatting.php */			return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );		}	}

Changelog

Introduced in 0.71. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.