PHP | getservbyport() Function
Last Updated :
30 Aug, 2019
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
$intservname = getservbyport (80, "tcp" );
echo $intservname ;
?>
|
Output:
http
Program 2:
<?php
$port = array (21, 22, 23, 25, 80);
foreach ( $port as $index ) {
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
Share your thoughts in the comments
Please Login to comment...