When working with domain names in PHP, it’s often crucial to verify their existence to ensure accurate and reliable operations. Whether you’re building a web application that requires valid domain names or performing domain-related tasks such as DNS lookups, checking if a domain name exists is an essential step. In this blog post, we will explore how to check if a domain name exists in PHP.
The PHP code checks if the domain name exists or not by using the gethostbyname() function. The gethostbyname() function takes a domain name as input and returns the IP address associated with that domain name. However, if the gethostbyname() function fails, it returns a string containing the provided domain name unmodified.
<?php
$domain = 'coderscratchpad.com';
if (gethostbyname($domain) !== $domain) {
echo "Domain name exists!";
} else {
echo "Domain name does not exist.";
}
It is a simple way to check whether a given domain name exists or not. I hope that’s been informative to you. If you wish to learn more about PHP, please subscribe to our newsletter today and continue your PHP learning journey with us!