PHP dns_check_record() Function: Everything You Need to Know
As a PHP developer, you may need to verify DNS records or check whether a DNS record exists. In such scenarios, the PHP dns_check_record() function comes in
As a PHP developer, you may need to verify DNS records or check whether a specific record exists for a domain — for example, to validate that an email domain can receive mail before accepting a sign-up. Historically, the dns_check_record() function was used for this purpose.
Important:
dns_check_record()was simply an alias ofcheckdnsrr(). The alias namedns_check_record()was deprecated in PHP 7.3 and removed in PHP 8.0 — butcheckdnsrr()itself is not deprecated and still works in every modern PHP version. So on PHP 8+ you have two ways forward: callcheckdnsrr()for a simple yes/no answer, or calldns_get_record()when you also need the record data.
This tutorial explains the legacy function and both modern alternatives.
What is the dns_check_record() Function?
The dns_check_record() function was a PHP built-in function that checked whether a specific type of DNS record existed for a given domain name. It returned a boolean value: true if at least one matching record existed, and false otherwise. Because the dns_check_record() name was removed in PHP 8.0, calling it on a modern installation triggers a fatal Call to undefined function error.
To migrate, do a literal find-and-replace: dns_check_record( becomes checkdnsrr(. The parameters and behavior are identical.
How to Use the dns_check_record() Function
Using the legacy dns_check_record() function was straightforward. Here is its syntax:
The syntax of PHP dns_check_record() Function
dns_check_record($host, $type);The function takes two parameters:
$host: The domain name that you want to check.$type: The type of DNS record that you want to check, as a string ("A","MX","NS", …). This parameter is optional and defaults to"MX"if not specified.
Option 1: checkdnsrr() — a simple yes/no check
If all you need is a boolean answer ("does this domain have a record of this type?"), checkdnsrr() is the direct drop-in replacement. Its signature is the same:
Option 2: dns_get_record() — when you need the record data
If you want the actual record values (IP addresses, mail-server hostnames, priorities) rather than just a boolean, use dns_get_record(). It returns an array of records when found, or an empty array on failure. Note that it uses DNS_* constants (e.g. DNS_A, DNS_MX), not the string types that checkdnsrr() expects:
<?php
$domain = "example.com";
$records = dns_get_record($domain, DNS_A);
if (!empty($records)) {
echo "DNS record exists for $domain";
echo PHP_EOL . "IP: " . $records[0]["ip"];
} else {
echo "DNS record does not exist for $domain";
}Here we check whether an A record exists and then read the resolved IPv4 address from the first record. Each element of the returned array is itself an associative array whose keys depend on the record type (ip for A, target and pri for MX, and so on).
Types of DNS Records
Both dns_check_record() and dns_get_record() can check various types of DNS records. The $type parameter specifies the type of DNS record that you want to check. Here are some of the most common types of DNS records:
A: The IPv4 address of the domain name.AAAA: The IPv6 address of the domain name.MX: The mail exchange (mail server) records for the domain name.NS: The authoritative name servers for the domain name.CNAME: The canonical name that an alias points to.TXT: Arbitrary text records, often used for SPF, DKIM and domain verification.ANY: Every available record type for the domain.
When you query an MX record specifically, the dedicated helper getmxrr() (and its alias dns_get_mx()) can populate the host list and priorities for you in one call.
Common Gotcha: Why a Lookup Might Fail
checkdnsrr() and dns_get_record() perform a live network DNS query, so results depend on the runtime environment:
- No network or a sandboxed host (such as many CI runners or shared-hosting jails) can make every lookup return
false/empty even for valid domains. - Transient DNS timeouts can cause intermittent failures. For validation logic, treat a single failure cautiously rather than rejecting a user immediately.
- Type matters: a domain may have
MXrecords but noArecord (or vice versa). Always query the record type you actually care about.
Because these functions hit the network, avoid calling them in tight loops; cache results where you can.
Conclusion
While the dns_check_record() name was removed in PHP 8.0, the functionality lives on: use checkdnsrr() for a quick boolean check, or dns_get_record() when you need the underlying record data. With either, you can reliably verify A, AAAA, MX, NS, CNAME and TXT records for any domain in modern PHP.