PHP | getservbyname() Function
Last Updated :
30 Aug, 2019
The getservbyname() function is an inbuilt function in PHP which returns the port number for given protocol and Internet service.
Syntax:
int getservbyname( string $service, string $protocol )
Parameters: This function accepts two parameters as mentioned above and described below:
- $protocol: It is required parameter. It specifies protocol name, like tcp, udp etc in string format.
- $service: It is required parameter. It specifies the Internet service name, like http int string format.
Return Value: This function returns the port number on success or False if service or protocol not found.
Note: This function is available for PHP 4.0.0 and newer version.
Below programs illustrate the getservbyname() function in PHP:
Program 1:
<?php
$portnum = getservbyname ( "http" , "tcp" );
echo $portnum ;
?>
|
Output:
80
Program 2: This program checks multiple services.
<?php
$services = array ( "ftp" , "ssh" ,
"telnet" , "http" , "https" );
foreach ( $services as $index ) {
echo getservbyname ( $index , "tcp" )
. ": " . $index . "<br>" ;
}
?>
|
Output:
21: ftp
22: ssh
23: telnet
80: http
443: https
Reference: https://www.php.net/manual/en/function.getservbyname.php
Share your thoughts in the comments
Please Login to comment...