PHP getservbyname() Function
The PHP getservbyname() function returns the Internet port number associated with an specified service for the specified protocol. The function returns false if the service or protocol is not found.
Syntax
getservbyname(service, protocol)
Parameters
service |
Required. Specify the Internet service name, as a string. |
protocol |
Required. Specify protocol which is either "tcp" or "udp" (in lowercase). |
Return Value
Returns the port number, or false if service or protocol is not found.
Example:
The example below shows the usage of getservbyname() function.
<?php //array containing services as string elements $services = array('http', 'ftp', 'ssh', 'telnet', 'imap', 'smtp', 'nicname', 'gopher', 'finger', 'pop3', 'www'); //finding the port number associated with //above services for "tcp" protocol foreach ($services as $service) { $port = getservbyname($service, 'tcp'); echo $service .": " . $port . "\n"; } ?>
The output of the above code will be:
http: 80 ftp: 21 ssh: 22 telnet: 23 imap: 143 smtp: 25 nicname: 43 gopher: 70 finger: 79 pop3: 110 www: 80
❮ PHP Network Reference