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 internet service protocol specified by service, based on /etc/services.

Return value

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

Parameters

ParametersDescription
serviceString of the internet service name.
protocolprotocol 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