PHP | getprotobynumber() Function

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”.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

tcp

Program 2: This program checking for many protocol name.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>";
}
?>

chevron_right


Output:

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

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



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.