<?php
$addresses = gethostbynamel('example.com') ?: [];
foreach ($addresses as $address) {
echo htmlspecialchars($address, ENT_QUOTES, 'UTF-8') . '<br>';
}
?>
gethostbyname() returns one IPv4 address, while gethostbynamel() returns all IPv4 addresses it finds for the host.
<?php
$ip = gethostbyname('example.com');
echo htmlspecialchars($ip, ENT_QUOTES, 'UTF-8');
?>
Mail exchanger records can be collected with getmxrr():
<?php
$mxHosts = [];
if (getmxrr('example.com', $mxHosts)) {
foreach ($mxHosts as $host) {
echo htmlspecialchars($host, ENT_QUOTES, 'UTF-8') . '<br>';
}
}
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.