What is the purpose of using fsockopen() in PHP to check the reachability of a website?

The purpose of using fsockopen() in PHP to check the reachability of a website is to establish a connection to the specified URL and port. This can be useful for testing if a website is accessible or if a specific service is running on a server.

$host = 'www.example.com';
$port = 80;
$timeout = 5;

$fp = @fsockopen($host, $port, $errno, $errstr, $timeout);

if (!$fp) {
    echo "Website is unreachable";
} else {
    echo "Website is reachable";
    fclose($fp);
}