What are the best practices for efficiently checking the online status of a server using PHP?

To efficiently check the online status of a server using PHP, you can use the `fsockopen` function to establish a connection to the server. By setting a timeout for the connection, you can quickly determine if the server is online or not.

function isServerOnline($server, $port) {
    $timeout = 1;
    $fp = @fsockopen($server, $port, $errno, $errstr, $timeout);
    if ($fp) {
        fclose($fp);
        return true;
    } else {
        return false;
    }
}

// Usage example
$server = 'example.com';
$port = 80;
if (isServerOnline($server, $port)) {
    echo 'Server is online';
} else {
    echo 'Server is offline';
}