Thursday, January 22, 2026
HomeLanguagesPHP | getservbyport() Function

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:




<?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

RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS