PHP getservbyport() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, getservbyport() looks up the Internet service name for a port number and protocol, using the same kind of table as /etc/services.

You pass a port such as 80 and a protocol of "tcp" or "udp". If the port is listed, you get a name like "http". If it is not found, the function returns false.

Syntax

Call getservbyport() with the values you need. Start with this line.

PHP
$name = getservbyport(80, "tcp");
CallWhat it does
getservbyport(80, "tcp")Returns "http" on a normal services table.
getservbyport(443, "tcp")Returns "https" for port 443 over TCP.
getservbyport(59999, "tcp")Returns false when no service is registered for that port.

This is the reverse of getservbyname(). Protocol must be lowercase "tcp" or "udp". Unlisted high ports usually return false.

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

Parameters

ParameterDescription
portRequired. The port number to look up, such as 80 or 22.
protocolRequired. The protocol name, usually "tcp" or "udp" (lowercase).

Return Value

Return ValueDescription
string|falseThe service name as a string, or false if the port or protocol is not found.

PHP Examples

Example 1 — Name Common TCP Ports

Look up a few well-known ports and print the service names.

PHP
<?php
foreach ([21, 22, 80, 443] as $port) {
    $name = getservbyport($port, "tcp");
    echo $port, " => ", var_export($name, true), PHP_EOL;
}

How It Works

  1. Each port is looked up with protocol "tcp".
  2. 21 maps to ftp and 22 to ssh on a normal table.
  3. 80 and 443 map to http and https.
  4. var_export() quotes the string names so the type is clear.

Example 2 — Port With No Service Name

Ask about a high port that is usually not listed in the services file.

PHP
<?php
$name = getservbyport(59876, "tcp");
var_dump($name);

How It Works

  1. 59876 is not a standard registered service on most hosts.
  2. getservbyport() returns false instead of a name.
  3. var_dump() shows bool(false).
  4. Treat that as "unknown name", not as "port is closed".

Example 3 — Round-Trip With getservbyname()

Convert a name to a port and back again to confirm the services table agrees with itself.

PHP
<?php
$port = getservbyname("smtp", "tcp");
$name = getservbyport($port, "tcp");
echo "port=", $port, " name=", $name, PHP_EOL;

How It Works

  1. getservbyname("smtp", "tcp") returns port 25.
  2. getservbyport(25, "tcp") returns the name smtp.
  3. The printed line shows both values together.
  4. This pattern is handy when teaching how the two functions relate.

Frequently Asked Questions

It returns the service name for a port number and protocol from the system services table. For example, getservbyport(80, "tcp") returns "http".
It means that port and protocol combination is not listed locally. It does not prove the remote port is unused.
Match the protocol you care about. Web ports use "tcp". Some services exist under both, with different meanings.
Yes. One turns a name into a port, the other turns a port into a name.
In the services table a port and protocol pair usually maps to one name. Different protocols can reuse the same number.

Next: header()

header() sends a raw HTTP header to the client. You must call it before any body output, s.

header() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful