What are the best practices for handling server status checks in PHP?
When handling server status checks in PHP, it is important to efficiently monitor the availability and performance of servers to ensure smooth operation of web applications. One best practice is to use a combination of server-side scripting and external monitoring tools to regularly check the status of servers and promptly address any issues that may arise.
// Example PHP code for handling server status checks
function checkServerStatus($serverUrl) {
    $ch = curl_init($serverUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    if ($httpCode == 200) {
        echo "Server is up and running!";
    } else {
        echo "Server is down. HTTP code: " . $httpCode;
    }
    
    curl_close($ch);
}
// Usage
$serverUrl = "http://example.com";
checkServerStatus($serverUrl);