PHP checkdnsrr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, checkdnsrr() asks DNS whether a hostname (or IP) has at least one record of a given type. It returns true when a matching record is found and false when none are found or the lookup fails.

The default record type is MX (mail exchanger). You can also ask for A, AAAA, NS, TXT and other common types. This is a quick yes-or-no check, not a full dump of the records.

Syntax

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

PHP
$ok = checkdnsrr("example.com", "MX");
CallWhat it does
checkdnsrr("example.com")Looks for MX records. Returns true if any exist.
checkdnsrr("example.com", "A")Looks for IPv4 A records for the hostname.
checkdnsrr("127.0.0.1", "PTR")Looks for a reverse PTR record for an IP address.

checkdnsrr() only answers yes or no. For the actual record data, use dns_get_record() or getmxrr(). On some Windows setups DNS functions need a working resolver; always treat false as "not found or lookup failed".

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

Parameters

ParameterDescription
hostnameRequired. The host name or IP address to look up in DNS.
typeOptional. The DNS record type to search for, such as "MX", "A", "AAAA", "NS", "TXT" or "ANY". The default is "MX".

Return Value

Return ValueDescription
booltrue if at least one matching DNS record is found; false if none are found or an error occurred.

PHP Examples

Example 1 — Check for MX Records

Ask whether a domain has MX records. The output below is a typical sample result, not a live lookup.

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

if (checkdnsrr($host, "MX")) {
    echo $host, " has MX records.", PHP_EOL;
} else {
    echo $host, " has no MX records (or lookup failed).", PHP_EOL;
}

How It Works

  1. We store the domain name in $host.
  2. checkdnsrr() looks for MX records for that host.
  3. true means at least one MX record was found.
  4. The printed line is a typical demo result for a well-known domain.

Example 2 — Check Several Record Types

Loop over common types and print whether each one is present. Sample answers are fixed for the tutorial.

PHP
<?php
$host = "example.com";
$types = ["A", "AAAA", "MX", "NS", "TXT"];

foreach ($types as $type) {
    $found = checkdnsrr($host, $type);
    echo $type, ": ", $found ? "yes" : "no", PHP_EOL;
}

How It Works

  1. The array lists common DNS record types.
  2. Each loop calls checkdnsrr() with a different $type.
  3. We print "yes" or "no" based on the boolean return value.
  4. Real answers depend on the domain and your DNS server; this output is a typical sample.

Example 3 — Guard Before Looking Up Mail Hosts

Use checkdnsrr() as a quick gate before calling getmxrr() for details.

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

if (!checkdnsrr($host, "MX")) {
    echo "No MX records to list.", PHP_EOL;
} else {
    getmxrr($host, $mxhosts);
    echo "First MX: ", $mxhosts[0], PHP_EOL;
}

How It Works

  1. checkdnsrr() first asks whether any MX records exist.
  2. If the answer is false, we skip the heavier lookup.
  3. getmxrr() fills $mxhosts with the mail server names.
  4. We print the first MX host from a typical sample list.

Frequently Asked Questions

checkdnsrr() checks DNS for records of a given type for a hostname or IP. It returns true if any matching records exist, otherwise false.
The default $type is "MX", so checkdnsrr("example.com") looks for mail exchanger records.
checkdnsrr() only returns true or false. dns_get_record() returns the actual record data as arrays, or false on failure.
Yes. dns_check_record() is an alias of checkdnsrr(). Both names call the same function.
No. It only checks DNS records for a domain. It does not prove that an inbox exists or that mail will be accepted.

Next: closelog()

closelog() closes the connection to the system logger that was opened with openlog(). Afte.

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