Open In App

PHP | getprotobynumber() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The getprotobynumber() function is an inbuilt function in PHP which returns the protocol name for a specified protocol number.

Syntax:

string getprotobynumber( int $protocol_number )

Parameters: This function accepts single parameter $protocol_number which is required. It specifies the protocol number, like 6 for tcp, 17 for udp etc.

Return Value: This function returns protocol name on success and FALSE on failure.

Note: This function is available for PHP 4.0.0 and newer version.

Below programs illustrate the getprotobynumber() function in PHP:

Program 1: This program uses protocol number for protocol name “tcp”.




<?php
  
// The getprotobynumber() function get protocol
// name associated with protocol number 
$protocolname = getprotobynumber(6);
  
// Display result
echo $protocolname;
?>


Output:

tcp

Program 2: This program checking for many protocol name.




<?php
  
// Store the protocol number in an array
$protocol_number  = array(6, 17, 20, 41);
  
foreach( $protocol_number as $number ){
      
    // The getprotobynumber() function get protocol
    // name associated with protocol number 
    echo $number . ": " . getprotobynumber($number)
            . "<br>";
}
?>


Output:

6: tcp
17: udp
20: hmp
41: ipv6

Reference: https://www.php.net/manual/en/function.getprotobynumber.php


Last Updated : 30 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads