PHP getprotobyname() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, getprotobyname() converts an internet protocol name such as "tcp" or "udp" into its protocol number. On failure it returns false.

Protocol numbers come from the system protocol database (for example /etc/protocols on Unix). The reverse conversion is getprotobynumber().

Syntax

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

PHP
$num = getprotobyname("tcp");
CallWhat it does
getprotobyname("tcp")Returns 6 on most systems.
getprotobyname("udp")Returns 17 on most systems.
getprotobyname("icmp")Returns 1 on most systems.

Names are usually lowercase in examples, but check your platform's protocol database. A false result means the name was not found.

Library declarations
PHP
getprotobyname(string $name): int|false

Parameters

ParameterDescription
nameRequired. The protocol name, such as "tcp", "udp" or "icmp".

Return Value

Return ValueDescription
intThe protocol number for the given name.
falseReturned when the protocol name is unknown.

PHP Examples

Example 1 — Look Up TCP and UDP Numbers

Resolve common protocol names. These numbers are standard on nearly all systems.

PHP
<?php
echo "tcp=", getprotobyname("tcp"), PHP_EOL;
echo "udp=", getprotobyname("udp"), PHP_EOL;

How It Works

  1. getprotobyname("tcp") reads the system protocol table.
  2. TCP is protocol number 6 on standard systems.
  3. UDP is protocol number 17.
  4. We print both values with clear labels.

Example 2 — Handle an Unknown Protocol Name

Show false when the name is not in the protocol database.

PHP
<?php
$num = getprotobyname("not-a-real-proto");

if ($num === false) {
    echo "unknown protocol", PHP_EOL;
} else {
    echo $num, PHP_EOL;
}

How It Works

  1. Pass a name that should not exist.
  2. The function returns false.
  3. Strict comparison detects failure.
  4. Print a clear message instead of using the false value as a number.

Example 3 — Map Several Names in a Loop

Convert a small list of protocol names to numbers for display.

PHP
<?php
$names = ["icmp", "tcp", "udp"];

foreach ($names as $name) {
    $num = getprotobyname($name);
    echo $name, " => ", ($num === false ? "n/a" : $num), PHP_EOL;
}

How It Works

  1. Store the names you care about in an array.
  2. Look up each name with getprotobyname().
  3. Replace false with "n/a" for readability.
  4. The numbers shown are the usual IANA/system values.

Frequently Asked Questions

It converts a protocol name like "tcp" into the matching protocol number, or returns false if the name is unknown.
From the operating system's protocol database (often /etc/protocols), not from a live network query.
getprotobynumber() converts a protocol number back into its name.
On standard systems yes, but always handle false and avoid hard-coding assumptions in portable code when possible.
No. It only reads protocol name/number mappings from the local system.

Next: getprotobynumber()

getprotobynumber() converts an internet protocol number into its name string, such as turn.

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