PHP getprotobynumber() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, getprotobynumber() converts an internet protocol number into its name string, such as turning 6 into "tcp". On failure it returns false.

Use it when you have a numeric protocol id and want a readable label. The reverse helper is getprotobyname().

Syntax

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

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

Pass an integer protocol number from the system database. Unknown numbers return false. Pair with getprotobyname() when you need round-trip conversions.

Library declarations
PHP
getprotobynumber(int $protocol): string|false

Parameters

ParameterDescription
protocolRequired. The protocol number to convert into a name.

Return Value

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

PHP Examples

Example 1 — Convert Common Protocol Numbers

Turn standard numbers into names. Results below match typical system databases.

PHP
<?php
echo getprotobynumber(6), PHP_EOL;
echo getprotobynumber(17), PHP_EOL;

How It Works

  1. 6 is the usual number for TCP.
  2. 17 is the usual number for UDP.
  3. getprotobynumber() returns the name strings.
  4. Each echo prints one name per line.

Example 2 — Unknown Number Returns false

Demonstrate failure handling for a number that is not mapped.

PHP
<?php
$name = getprotobynumber(99999);

var_dump($name);

How It Works

  1. 99999 is not a normal protocol number.
  2. The function returns false.
  3. var_dump() shows the boolean failure clearly.
  4. Check for false before printing the name.

Example 3 — Round-Trip with getprotobyname()

Convert name → number → name to show the two helpers working together.

PHP
<?php
$num = getprotobyname("tcp");
$name = getprotobynumber($num);

echo $num, " ", $name, PHP_EOL;

How It Works

  1. getprotobyname("tcp") returns the number 6.
  2. getprotobynumber(6) returns the name "tcp".
  3. Printing both confirms the round trip.
  4. This pattern is useful when normalizing protocol values.

Frequently Asked Questions

It converts a protocol number into its name string, such as 6 → "tcp", or returns false if the number is unknown.
Use getprotobyname(), which is the reverse of getprotobynumber().
No. It reads the local protocol database only.
Pass an int protocol number. Strings that are not numeric will not match useful entries.
That number is missing from the system's protocol database, or the platform uses a different mapping. Handle false explicitly.

Next: getservbyname()

getservbyname() looks up the port number for a named Internet service and protocol, using .

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