Are there alternative methods or best practices for obtaining host information in PHP to avoid potential errors with gethostbyaddr()?

The issue with using gethostbyaddr() in PHP is that it can potentially lead to errors or slow performance due to network delays or DNS resolution issues. To avoid these problems, an alternative method is to use the gethostbynamel() function, which returns an array of IPv4 addresses corresponding to a given hostname. This can provide more reliable and faster results compared to gethostbyaddr().

$hostname = "www.example.com";
$ip_addresses = gethostbynamel($hostname);

if ($ip_addresses !== false) {
    foreach ($ip_addresses as $ip) {
        echo "IP address for $hostname: $ip\n";
    }
} else {
    echo "Failed to retrieve IP address for $hostname\n";
}