PHP getservbyname() Function: Everything You Need to Know
As a PHP developer, you may need to obtain the port number associated with a given service name. In such scenarios, the PHP getservbyname() function comes in
Network services are identified by well-known names such as http, ftp, or smtp, but the operating system actually routes traffic by port number (80, 21, 25, and so on). PHP's getservbyname() function looks up that port number for you, reading the same service database the rest of the system uses. This page explains the syntax, what the database is, common use cases, and the gotchas to watch for.
Syntax
getservbyname(string $service, string $protocol): int|falseThe function takes two required parameters:
$service— the Internet service name, for example"http","ssh", or"smtp".$protocol— the transport protocol, either"tcp"or"udp". Unlike a lot of older documentation suggests, this argument is not optional; you must pass it.
It returns the port number as an integer on success, or false if the service/protocol pair isn't found. Because false and a valid port can both look "falsy-adjacent," always compare with the strict === operator when checking for failure.
Where the data comes from
getservbyname() does not connect to anything or guess. It reads a local lookup table:
- On Linux and macOS this is the
/etc/servicesfile. - On Windows it is
%SystemRoot%\system32\drivers\etc\services.
If a service is missing from that file, the lookup fails even though the service "obviously" exists. The result is therefore a property of the machine running the script, not a universal constant.
A basic example
The === false check matters: it cleanly separates a real failure from a legitimate result like port 0 (which would be falsy under a loose == comparison).
Looking up several services
In practice you often resolve a batch of names at once — for example to build a firewall report or to validate config:
<?php
$services = ["http", "https", "ftp", "smtp", "ssh"];
foreach ($services as $name) {
$port = getservbyname($name, "tcp");
echo $port === false
? "{$name}: not found\n"
: "{$name}: {$port}\n";
}
/*
Output:
http: 80
https: 443
ftp: 21
smtp: 25
ssh: 22
*/TCP vs UDP
The same service name can resolve to different entries per protocol, and some services only exist for one of them. Always pass the protocol you actually care about:
<?php
var_dump(getservbyname("domain", "tcp")); // int(53) — DNS over TCP
var_dump(getservbyname("domain", "udp")); // int(53) — DNS over UDP
var_dump(getservbyname("ntp", "udp")); // int(123)
var_dump(getservbyname("madeup", "tcp")); // bool(false) — unknown serviceWhen would I use this?
- Building network clients dynamically. Resolve a service name to a port before opening a socket with
fsockopen()orpfsockopen(), so your code reads"smtp"instead of a magic25. - Validating configuration. Confirm a service name from a config file is actually known to the host before using it.
- Reporting / tooling. Translate human-friendly service names into ports for logs, dashboards, or firewall rules.
Related functions
getservbyport()— the inverse: given a port number and protocol, return the service name.getprotobyname()— look up a protocol number (e.g.tcp→6) by its name.
Common gotchas
- Both arguments are required. Omitting
$protocolraises an error; there is no implicit default. - Strict comparison only. Use
=== falseto detect failure so you don't misread port0or a numeric-string edge case. - Host-dependent results. The answer comes from the local services database; a name unknown to one machine may resolve on another.
- It is offline. No DNS, no network — purely a local table lookup, so it is fast and safe to call frequently.
Summary
getservbyname() maps a service name and protocol to a port number using the operating system's services database, returning an integer port or false when the pair is unknown. Pair it with getservbyport() for the reverse direction and with fsockopen() when you need to turn that port into an actual connection.