English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP getservbyname() Function Usage and Example

    PHP HTTP  Reference Manual

The getservbyname() function retrieves the port corresponding to the Internet service protocol.

Syntax

int getservbyname ( string $service , string $protocol )

Definition and Usage

 getservbyname() returns the port corresponding to the protocol specified by the Internet service service, based on /etc/services.

Return value

 Returns the port number, or FALSE if the service or protocol is not found.

Parameters

ParametersDescription
serviceString representing the name of the Internet service.
protocolThe protocol can be either "tcp" or "udp" (lowercase).

Example

Try the following example to get the port corresponding to the Internet service protocol :

<?php
$services = array('http', 'ftp', 'ssh', 'telnet', 'imap','smtp', 'nicname', 'gopher', 'finger', 'pop3', 'www');
foreach ($services as $service) {
    $port = getservbyname($service, 'tcp');
    echo $service . ": " . $port . "<br />\n";
}
?>

PHP HTTP  Reference Manual