PHP | getservbyport() Function

The getservbyport() function is an inbuilt function in PHP which returns the Internet service for given protocol and port number.

Syntax:

string getservbyport( int $port, string $protocol)

Parameters: This function accepts two parameters as mentioned above and described below:



  • $protocol: It is required parameter. It specifies the protocol name, like tcp, udp etc.
  • $port: It is required parameter. It specifies the port number, like 80.

Return Value: This function returns the Internet Service name on success.

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

Below programs illustrate the getservbyport() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Use getservbyport() function to get
// the Internet service which corresponds
// to port and protocol
$intservname = getservbyport(80, "tcp");
  
// Display the output
echo $intservname;
  
?>

chevron_right


Output:

http

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Create an array of port numbers
$port = array(21, 22, 23, 25, 80);
  
// Loop run for each services
foreach( $port as $index) {
      
    // Use getservbyport() function to get
    // the Internet service which corresponds
    // to port and protocol
    echo $index . ": " .getservbyport($index, "tcp")
            . "<br>";
}
  
?>

chevron_right


Output:

21: ftp
22: ssh
23: telnet
25: smtp
80: http

Reference: https://www.php.net/manual/en/function.getservbyport.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.