Content · Web Development · PHP

PHP dns_get_record() Function: Usage and Examples

Queries DNS record data for a hostname and requested record type, returning a list of matching records.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

In PHP, dns_get_record() helps with runtime and application data. Queries DNS record data for a hostname and requested record type, returning a list of matching records.

What Is dns_get_record() in PHP?


The function or method has a specific purpose: Queries DNS record data for a hostname and requested record type, returning a list of matching records. The following examples show the main call, one additional situation, and an application-style use.

Syntax:

<?php
dns_get_record(string $hostname, int $type = ..., $authoritative_name_servers = ..., $additional_records = ..., bool $raw = ...) : array|false

Parameters and Return Value


  • $hostname (string) — argument used by this API to perform its operation; required.
  • $type (int) — requested type or type selector; optional.
  • $authoritative_name_servers (mixed) — argument used by this API to perform its operation; optional.
  • $additional_records (mixed) — argument used by this API to perform its operation; optional.
  • $raw (bool) — argument used by this API to perform its operation; optional.

Example 1: Basic Usage


Queries DNS record data for a hostname and requested record type, returning a list of matching records.

<?php
$records = dns_get_record("example.com", DNS_MX);
foreach ($records as $record) { echo $record["target"] . "\n"; }

This example demonstrates the central operation. The final output depends on the supplied input and, where relevant, the runtime or connected service.

Example 2: Handling a Different Case


<?php
$records = dns_get_record("example.net", DNS_MX);
foreach ($records as $record) { echo $record["target"] . "\n"; }

The second example focuses on a different input or failure condition, so the behavior can be distinguished from the basic case.

Practical Example


<?php
$records = dns_get_record("example.org", DNS_MX);
foreach ($records as $record) { echo $record["target"] . "\n"; }

The practical example demonstrates how the same subject fits into a larger application. Use your own file paths, endpoints and validated inputs rather than copying sample settings directly.

Important Behavior and Edge Cases


  • Queries DNS record data for a hostname and requested record type, returning a list of matching records.
  • Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available. Network outcomes are not deterministic and responses should not be assumed to be successful.
  • Use the documented return type and handle false, null and exceptions according to the specific call.

Comparison With Related Functions


ApproachDifference
dns_get_recordQueries DNS record data for a hostname and requested record type, returning a list of matching records.
function_existsChecks that a PHP function name is available; does not perform this operation.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring errors from dns_get_recordCheck the actual result and handle the expected error path.
Using the demonstration unchanged in productionAdapt file paths, configuration, authentication and permission rules to the application.
Treating input or returned data as trustedApply validation at input boundaries and context-aware escaping at output boundaries.

Key Takeaways


  • Queries DNS record data for a hostname and requested record type, returning a list of matching records.
  • Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available. Network outcomes are not deterministic and responses should not be assumed to be successful.
  • Use the documented return type and handle false, null and exceptions according to the specific call.

Queries DNS record data for a hostname and requested record type, returning a list of matching records. Use this approach when its capabilities and constraints match the application. Handle invalid data, failure conditions and resource cleanup explicitly.

Share this resourceSend this page to someone who may find it useful.