W3docs

PHP getservbyport() Function: Everything You Need to Know

As a PHP developer, you may need to obtain the service name associated with a given port number. In such scenarios, the PHP getservbyport() function comes in

The PHP getservbyport() function turns a numeric network port into the human-readable service name registered for it — for example, port 80 becomes http and port 443 becomes https. It is the inverse of getservbyname(), which goes the other way (name → port). This page explains the syntax, where the data comes from, common pitfalls, and how to use the function in real code.

What getservbyport() does

getservbyport() looks up the well-known service name that an operating system associates with a given port/protocol pair. It does not open a socket, contact a server, or scan anything — it simply reads a local lookup table maintained by the OS:

  • On Linux and macOS, the table is the /etc/services file.
  • On Windows, it is %WINDIR%\System32\drivers\etc\services.

Because the answer comes from a static file, the function is fast and works offline, but it only knows about ports that are listed in that file. A custom application port (say 8080 running your dev server) will usually return false because no service name is registered for it.

Syntax

getservbyport(int $port, string $protocol): string|false

Parameters

  • $port — the port number to look up, as an integer (for example 80, 443, 22).
  • $protocol — the protocol name, either "tcp" or "udp". Unlike some references suggest, this argument is required, not optional. The same port number can map to different service names depending on protocol.

Return value

  • The service name as a string (for example "http") when a match is found.
  • false when the port/protocol pair is not registered. Always test the result with the strict === false comparison, because a valid service name could otherwise be mistaken for a falsy value.

Basic example

php— editable, runs on the server

Here we ask for the TCP service on port 80. On a standard system the lookup succeeds and prints:

The service name for port number 80 and protocol name tcp is http

Because we compare with === false, an unregistered port is reported cleanly instead of producing a confusing message.

Looking up several ports

When you have a list of ports — for instance to label entries in a firewall log — loop over them and resolve each one:

<?php

$ports = [22, 25, 53, 443, 49152];

foreach ($ports as $port) {
  $service = getservbyport($port, "tcp");
  echo $service === false
    ? "Port $port/tcp: unknown service\n"
    : "Port $port/tcp: $service\n";
}

Typical output (port 49152 lies in the private/dynamic range and is not a registered service, so it falls through to the "unknown" branch):

Port 22/tcp: ssh
Port 25/tcp: smtp
Port 53/tcp: domain
Port 443/tcp: https
Port 49152/tcp: unknown service

When to use it (and when not to)

Use getservbyport() when you want a friendly label for a known port: rendering log files, building a network dashboard, or validating configuration. It is not a way to check whether a port is open or in use — it never touches the network. To test connectivity you need a real socket function such as fsockopen().

Common pitfalls

  • Forgetting the protocol argument. getservbyport(80) raises an error; always pass "tcp" or "udp".
  • Expecting custom ports to resolve. Ports outside /etc/services return false even if a server is happily listening on them.
  • Loose comparison. Use === false, not == false or !$result, so empty-string edge cases are not misread.

Conclusion

getservbyport() is a small but handy helper that translates a port/protocol pair into its registered service name by reading the system's services file. Remember that the protocol argument is mandatory, that unknown ports return false, and that the function performs a local lookup rather than any network activity. Pair it with getservbyname() for the reverse direction and with fsockopen() when you actually need to talk to a service.

Practice

Practice
What does the getservbyport() function in PHP do?
What does the getservbyport() function in PHP do?
Was this page helpful?