Use gethostbyaddr() when you need to attempts a reverse DNS lookup for an IPv4 address in a PHP application. The examples below show the actual operation and its limitations.
What Is gethostbyaddr() in PHP?
Attempts a reverse DNS lookup for an IPv4 address. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
gethostbyaddr(string $ip): string|false
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
$result = gethostbyaddr("127.0.0.1"); echo is_string($result) ? "Lookup attempted" : "Failed";
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Example 2: Different Input or Behavior
<?php
echo gethostbyaddr("192.0.2.1") !== false ? "Result available" : "No result";
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Practical Example: Applying the Function
<?php
$ipAddress = "127.0.0.1"; $hostname = gethostbyaddr($ipAddress); echo $hostname === $ipAddress ? "No reverse name" : "Reverse name found";
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Important Behavior and Edge Cases
Reverse DNS is not proof of identity; lookup results depend on DNS and network access.
Comparison With Related Functions
| Approach | Difference |
|---|---|
gethostbyaddr() | Attempts a reverse DNS lookup for an IPv4 address. |
| Related operation | gethostbyname() resolves a hostname toward an IPv4 address. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Reverse DNS is not proof of identity; lookup results depend on DNS and network access. |
| Confusing this function with a related operation | gethostbyname() resolves a hostname toward an IPv4 address. |
Key Takeaways
- Attempts a reverse DNS lookup for an IPv4 address.
- Reverse DNS is not proof of identity; lookup results depend on DNS and network access.
- gethostbyname() resolves a hostname toward an IPv4 address.
Use gethostbyaddr() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.