What are some alternative methods to check if a specific port is associated with a protocol in PHP?

When working with networking in PHP, it may be necessary to check if a specific port is associated with a particular protocol. One way to do this is by using the getservbyport() function provided by PHP. This function takes a port number as input and returns the corresponding service name associated with that port. By checking if the returned service name matches the desired protocol, you can determine if the port is associated with the protocol.

$port = 80;
$protocol = 'http';

$service = getservbyport($port, 'tcp');

if ($service === $protocol) {
    echo "Port $port is associated with $protocol protocol.";
} else {
    echo "Port $port is not associated with $protocol protocol.";
}