PHP getservbyname() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, getservbyname() looks up the port number for a named Internet service and protocol, using the same kind of table as /etc/services on Unix-like systems.

You pass a service name such as "http" or "ftp" and a protocol of "tcp" or "udp". If the name is known, you get the port as an integer. If it is not found, the function returns false.

Syntax

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

PHP
$port = getservbyname("http", "tcp");
CallWhat it does
getservbyname("http", "tcp")Returns 80 on a normal services table.
getservbyname("https", "tcp")Returns 443 for HTTPS over TCP.
getservbyname("no-such-svc", "tcp")Returns false when the service name is unknown.

Service and protocol names are lowercase strings. The exact list depends on the host operating system, but common names like http, https, ftp, ssh and smtp are almost always present.

Library declarations
PHP
getservbyname(string $service, string $protocol): int|false

Parameters

ParameterDescription
serviceRequired. The Internet service name, such as "http", "ftp" or "ssh".
protocolRequired. The protocol name, usually "tcp" or "udp" (lowercase).

Return Value

Return ValueDescription
int|falseThe port number as an integer, or false if the service or protocol is not found.

PHP Examples

Example 1 — Look Up Common TCP Ports

Ask getservbyname() for a few everyday service names and print the ports.

PHP
<?php
foreach (["http", "https", "ftp", "ssh"] as $service) {
    $port = getservbyname($service, "tcp");
    echo $service, " => ", var_export($port, true), PHP_EOL;
}

How It Works

  1. Each service name is looked up with protocol "tcp".
  2. http maps to port 80 and https to 443 on a normal services table.
  3. ftp and ssh map to 21 and 22.
  4. var_export() prints the integer clearly so you can see the type.

Example 2 — Unknown Service Name

See what happens when the service name is not in the system table.

PHP
<?php
$port = getservbyname("totally-fake-service", "tcp");
var_dump($port);

How It Works

  1. The made-up name is not listed in /etc/services style data.
  2. getservbyname() returns false instead of a port number.
  3. var_dump() shows bool(false) so you can tell failure from port 0.
  4. Always test the return value before opening a connection.

Example 3 — Build a URL From a Service Name

Resolve the port first, then build a simple host:port string for a demo connection target.

PHP
<?php
$host = "example.com";
$service = "https";
$port = getservbyname($service, "tcp");

if ($port === false) {
    echo "Unknown service", PHP_EOL;
} else {
    echo $host, ":", $port, PHP_EOL;
}

How It Works

  1. getservbyname("https", "tcp") returns 443.
  2. We compare with === false so a real port is never treated as failure.
  3. The host and port are joined with a colon for display.
  4. In real code you would pass that port to fsockopen() or a similar call.

Frequently Asked Questions

It returns the port number for a named Internet service and protocol, using the same kind of table as /etc/services. For example, getservbyname("http", "tcp") returns 80.
Use lowercase "tcp" or "udp". Those are the values the services table expects.
The function returns false. Check the result before you use it as a port number.
getservbyname() turns a name into a port. getservbyport() turns a port number into a service name.
Common ports are the same almost everywhere, but custom or rare names depend on the host operating system table.

Next: getservbyport()

getservbyport() looks up the Internet service name for a port number and protocol, using t.

getservbyport() 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