What are the best practices for setting the timeout parameter when using the ping command in PHP?

When using the ping command in PHP, it is important to set a timeout parameter to prevent the script from hanging indefinitely if the pinged host is unreachable. The timeout parameter specifies the maximum time to wait for a response before considering the ping request failed. A common practice is to set the timeout to a reasonable value, such as 1 second, to balance between waiting for a response and not causing the script to hang for too long.

<?php

$host = 'example.com';
$timeout = 1; // Set timeout to 1 second

exec("ping -c 1 -W $timeout $host", $output, $result);

if ($result === 0) {
    echo "Ping successful!";
} else {
    echo "Ping failed!";
}