How can the fsockopen function in PHP be used to check the status of a server using UDP instead of TCP?

To check the status of a server using UDP instead of TCP in PHP, you can use the fsockopen function with the UDP protocol. This allows you to send a UDP packet to the server and check for a response. By specifying "udp://" in the fsockopen function, you can establish a UDP connection to the server and check its status.

$server = 'udp://example.com';
$port = 1234;
$timeout = 5;

$socket = fsockopen($server, $port, $errno, $errstr, $timeout);

if (!$socket) {
    echo "Server is not reachable";
} else {
    echo "Server is reachable";
}

fclose($socket);