W3docs

PHP getprotobyname() Function: Everything You Need to Know

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

When you work with low-level networking in PHP, you sometimes have a protocol name such as tcp or udp but need its numeric protocol number — the value assigned to that protocol by the IANA and used inside IP packet headers. The getprotobyname() function performs exactly that lookup. This page covers its syntax, return value, common protocol numbers, error handling, and how it relates to the other protocol/service lookup functions in PHP.

What is the getprotobyname() Function?

The getprotobyname() function is a built-in PHP function that maps a protocol name to its corresponding number. It reads the system's protocol database — typically /etc/protocols on Unix-like systems and %SystemRoot%\System32\drivers\etc\protocol on Windows — so the names it understands are the ones listed there (for example ip, icmp, tcp, udp, ipv6).

It is the inverse of getprotobynumber(), which turns a number back into a name. You typically need a raw protocol number when opening a raw socket with socket_create() and similar low-level APIs, where the protocol must be specified numerically.

Syntax

getprotobyname(string $protocol): int|false

The function takes one parameter:

  • $protocol — the protocol name to look up. The lookup is case-insensitive, so "TCP" and "tcp" return the same result.

It returns an integer representing the protocol number, or false if the protocol name is not found in the system database.

Basic example

Here is how to look up the number for the tcp protocol and handle the case where the lookup fails:

php— editable, runs on the server

For "tcp" this prints:

The protocol number for protocol name tcp is 6

Note the strict comparison === false. Because the smallest valid protocol number is 0 (the ip protocol), a loose check like if (!$protocol_number) would wrongly treat a successful ip lookup as a failure. Always compare against false with ===.

Common protocol numbers

These are the values you will see most often. They come straight from the system protocol database, so they are stable across platforms:

Protocol nameNumberMeaning
ip0Internet Protocol
icmp1Internet Control Message Protocol
tcp6Transmission Control Protocol
udp17User Datagram Protocol
ipv641Internet Protocol v6

Looking up several protocols

You can loop over a list of names and report each result, treating unknown names gracefully:

<?php

$protocols = ["tcp", "udp", "icmp", "bogus"];

foreach ($protocols as $name) {
    $number = getprotobyname($name);

    if ($number === false) {
        echo "$name: not found\n";
    } else {
        echo "$name => $number\n";
    }
}

This outputs:

tcp => 6
udp => 17
icmp => 1
bogus: not found

When would I use this?

In day-to-day web development you rarely need protocol numbers — fopen(), cURL, and stream wrappers all take URLs, not raw numbers. You reach for getprotobyname() when you go one level lower:

  • Creating raw sockets. socket_create(AF_INET, SOCK_RAW, getprotobyname("icmp")) writes the protocol number into the socket. Hard-coding 1 works but getprotobyname("icmp") documents intent and stays correct if the database differs.
  • Building or parsing IP packets, where the protocol field is numeric.
  • Network tooling and diagnostics that report both the name and number of a protocol.

Conclusion

getprotobyname() resolves a protocol name to the numeric protocol number defined in the system database, returning false when the name is unknown. Remember to test the result with a strict === false comparison so the valid ip value of 0 is not mistaken for an error, and reach for it whenever a low-level networking API expects a numeric protocol rather than a name.

Practice

Practice
What is the purpose of the getprotobyname function in PHP, according to the information provided on https://www.w3docs.com/learn-php/getprotobyname.html?
What is the purpose of the getprotobyname function in PHP, according to the information provided on https://www.w3docs.com/learn-php/getprotobyname.html?
Was this page helpful?