wppaste
WordPress

PHPMailer::isValidHost( string $host ): bool

Source
wp-includes/PHPMailer/PHPMailer.php:4536
Validate whether a string contains a valid value to use as a hostname or IP address.

Description

IPv6 addresses must include [], e.g. [::1], not just ::1.

Compatibility

WordPress
core
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

$hoststring
The host name or IP address to check

Return value

bool

Performance profile

How much work a call to PHPMailer::isValidHost() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
4–32

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 2 distinct outcomes

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

WhenInstructionsCalls it makes
always4–11none
!empty($host) && is_string($host) && $host24–32filter_var()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev494–328
8.5494–328
8.4494–32817 fewer instructions than PHP 8.3
8.3664–468
8.2664–468
8.1664–468
7.4664–468

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.

Source code

    public static function isValidHost($host)    {        //Simple syntax limits        if (            empty($host)            || !is_string($host)            || strlen($host) > 256            || !preg_match('/^([a-z\d.-]*|\[[a-f\d:]+\])$/i', $host)        ) {            return false;        }        //Looks like a bracketed IPv6 address        if (strlen($host) > 2 && substr($host, 0, 1) === '[' && substr($host, -1, 1) === ']') {            return filter_var(substr($host, 1, -1), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;        }        //If removing all the dots results in a numeric string, it must be an IPv4 address.        //Need to check this first because otherwise things like `999.0.0.0` are considered valid host names        if (is_numeric(str_replace('.', '', $host))) {            //Is it a valid IPv4 address?            return filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;        }        //Is it a syntactically valid hostname (when embedded in a URL)?        return filter_var('https://' . $host, FILTER_VALIDATE_URL) !== false;    }

Changelog

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 7.1.0 tag, from src/wp-includes/PHPMailer/PHPMailer.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.