PHP gethostbyaddr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, gethostbyaddr() performs a reverse DNS lookup: you give it an IP address and it returns the host name associated with that address, or false on failure.

This is useful for logs and diagnostics when you already have an IP. Forward lookups (name to IP) use gethostbyname() or gethostbynamel() instead.

Syntax

Call gethostbyaddr() with the values you need. Start with this line.

PHP
$host = gethostbyaddr("93.184.216.34");
CallWhat it does
gethostbyaddr("127.0.0.1")Often returns "localhost" on local machines.
gethostbyaddr("93.184.216.34")Returns a host name string when reverse DNS exists.
gethostbyaddr("0.0.0.0")Returns false when the reverse lookup fails.

Pass a dotted IPv4 address string (or a supported IPv6 address where available). On failure the function returns false, unlike gethostbyname() which returns the original name unchanged.

Library declarations
PHP
gethostbyaddr(string $ip): string|false

Parameters

ParameterDescription
ipRequired. The IP address to resolve to a host name.

Return Value

Return ValueDescription
stringThe host name on success.
falseReturned when the reverse lookup fails.

PHP Examples

Example 1 — Reverse Lookup for a Known IP

Resolve an IP to a host name. The printed name is a typical sample result.

PHP
<?php
$ip = "93.184.216.34";
$host = gethostbyaddr($ip);

if ($host === false) {
    echo "Lookup failed", PHP_EOL;
} else {
    echo $host, PHP_EOL;
}

How It Works

  1. We start with a public IPv4 address string.
  2. gethostbyaddr() asks DNS for a PTR/host name.
  3. false means the reverse lookup failed.
  4. "example. com" is shown as a typical sample answer.

Example 2 — Handle Localhost

Look up 127.0.0.1, which usually maps to localhost on developer machines.

PHP
<?php
$host = gethostbyaddr("127.0.0.1");
echo $host === false ? "failed" : $host;

How It Works

  1. 127. 0.0.1 is the IPv4 loopback address.
  2. Many systems map it to the name localhost.
  3. We print "failed" only when the call returns false.
  4. Your machine may return a different local name; this is a common sample.

Example 3 — Log IP and Host Together

Build a simple log line that includes both the IP and the reverse name when available.

PHP
<?php
$ip = "93.184.216.34";
$host = gethostbyaddr($ip);

$label = ($host === false) ? "(no reverse DNS)" : $host;
echo $ip, " => ", $label, PHP_EOL;

How It Works

  1. Store the IP you want to describe.
  2. Run the reverse lookup once.
  3. Fall back to a clear placeholder when the lookup fails.
  4. Print IP and label on one line for logs.

Frequently Asked Questions

It converts an IP address into a host name using reverse DNS. On failure it returns false.
gethostbyaddr() maps IP → name. gethostbyname() maps name → IPv4 address.
There was no reverse DNS record, or the lookup failed. Many IPs have no PTR record.
No. It expects an IP address string. Pass host names to gethostbyname() or gethostbynamel().
Not by itself. Attackers can sometimes control reverse DNS. Verify with a forward lookup if the name matters for security.

Next: gethostbyname()

gethostbyname() resolves a host name to a single IPv4 address string. On failure it return.

gethostbyname() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful