Have see this issue before , You are using a DNS service that resolves non-existent domains to a server that gives you a "friendly" error page, which it returns with a 200 response code.
See : get_headers Inconsistency for more details of this issue.
You can resolve it with DavidRandom solution
var_dump(ping("stackoverflow.com", 30, 1)); // port 30 return false
var_dump(ping("stackoverflow.com", 80, 1)); // port 80; returns 0.49000000953674
Your Function Modified
function ping($host, $port, $timeout) {
$a = gethostbyname('idontexist.tld');
$b = gethostbyname($host);
if ($a == $b)
return false;
$time = microtime(true);
$fp = @fsockopen($host, $port, $errCode, $errStr, $timeout);
$time = microtime(true) - $time;
if ($fp) {
fclose($fp);
return $time;
} else {
return false;
}
}