What are the differences between using fsockopen and fopen in PHP for checking server status?

When checking server status in PHP, using fsockopen allows for more control and flexibility in handling network connections compared to fopen. fsockopen can be used to establish a connection to a server and check its status by sending a request and receiving a response. On the other hand, fopen is primarily used for file operations and may not be suitable for checking server status.

<?php
$server = 'example.com';
$port = 80;
$timeout = 5;

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

if ($fp) {
    echo 'Server is up and running.';
    fclose($fp);
} else {
    echo 'Server is down or unreachable.';
}
?>