Example 1 — Name Common TCP Ports
Look up a few well-known ports and print the service names.
<?php
foreach ([21, 22, 80, 443] as $port) {
$name = getservbyport($port, "tcp");
echo $port, " => ", var_export($name, true), PHP_EOL;
} How It Works
- Each port is looked up with protocol "tcp".
- 21 maps to ftp and 22 to ssh on a normal table.
- 80 and 443 map to http and https.
- var_export() quotes the string names so the type is clear.

