getservbyname()
getservbyname() — Get port number associated with an Internet service and protocol.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
getservbyname() returns the Internet port which corresponds to service for the specified protocol as per /etc/services.
Signature
getservbyname(string $service, string $protocol): int|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $service | string | The Internet service name, as a string. |
| $protocol | string | protocol is either "tcp" or "udp" (in lowercase). |
Return value
Returns the port number, or false if service or protocol is not found.
Example
A runnable snippet:
$services = array('http', 'ftp', 'ssh', 'telnet', 'imap', 'smtp', 'nicname', 'gopher', 'finger', 'pop3', 'www'); foreach ($services as $service) { $port = getservbyname($service, 'tcp'); echo $service . ": " . $port . "<br />\n"; }
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page getservbyname() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.