PHP dns_check_record() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, dns_check_record() is an alias of checkdnsrr(). It checks whether DNS has records of a given type for a hostname or IP and returns true or false.

Use whichever name you prefer; both call the same code. The default type is MX. Prefer checkdnsrr() in new code because it is the primary name in the PHP manual.

Syntax

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

PHP
$ok = dns_check_record("example.com", "A");
CallWhat it does
dns_check_record("example.com")Same as checkdnsrr(): looks for MX records by default.
dns_check_record("example.com", "NS")Returns true if name server records exist.
dns_check_record("example.com", "TXT")Returns true if TXT records exist.

dns_check_record() is only an alias. Signatures, parameters and return values match checkdnsrr() exactly. Prefer one name in a project so readers are not confused.

Library declarations
PHP
dns_check_record(string $hostname, string $type = "MX"): bool

Parameters

ParameterDescription
hostnameRequired. The host name or IP address to look up.
typeOptional. DNS record type such as "MX", "A", "AAAA" or "TXT". Default is "MX".

Return Value

Return ValueDescription
booltrue if matching records are found; false if none are found or an error occurred.

PHP Examples

Example 1 — Alias Check for A Records

Call dns_check_record() the same way you would call checkdnsrr(). Sample output is fixed.

PHP
<?php
if (dns_check_record("example.com", "A")) {
    echo "A record found", PHP_EOL;
} else {
    echo "No A record (or lookup failed)", PHP_EOL;
}

How It Works

  1. dns_check_record() looks for A records for example. com.
  2. true means at least one A record exists.
  3. false would mean none found or a lookup error.
  4. The printed text is a typical tutorial sample.

Example 2 — Compare Both Names

Show that the alias and the main function return the same boolean for the same inputs.

PHP
<?php
$host = "example.com";
$type = "MX";

$a = checkdnsrr($host, $type);
$b = dns_check_record($host, $type);

var_dump($a === $b);
echo $a ? "MX present" : "MX missing";

How It Works

  1. Both functions receive the same host and type.
  2. We compare the booleans with ===.
  3. They match because dns_check_record() is an alias.
  4. The second line shows a typical MX-present sample.

Example 3 — Quick Domain Gate

Use the alias in a small helper that only continues when DNS answers yes.

PHP
<?php
function has_dns(string $host, string $type = "MX"): bool {
    return dns_check_record($host, $type);
}

echo has_dns("example.com", "NS") ? "NS ok" : "NS missing";

How It Works

  1. The helper wraps dns_check_record() for reuse.
  2. We ask for NS records on example. com.
  3. The return value is a simple boolean.
  4. "NS ok" is a typical sample for a public domain.

Frequently Asked Questions

dns_check_record() is an alias of checkdnsrr(). It returns true if DNS has records of the requested type for a host.
Both work the same. Prefer checkdnsrr() in new code because it is the primary documented name.
Common values include A, AAAA, MX, NS, SOA, PTR, CNAME, TXT and ANY. The default is MX.
No. It only returns true or false. Use dns_get_record() or getmxrr() when you need the data.
Either no matching records exist, or the DNS lookup failed. Check your network and resolver settings before assuming the domain is wrong.

Next: dns_get_mx()

dns_get_mx() is an alias of getmxrr(). It fills an array with MX host names for a domain a.

dns_get_mx() 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