Open In App

PHP | getservbyport() Function

Last Updated : 30 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

http

Program 2:




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


Output:

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

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads