What potential pitfalls should be considered when using ping commands for network monitoring in PHP?

One potential pitfall when using ping commands for network monitoring in PHP is the lack of error handling. If the ping command fails for any reason, it could cause the PHP script to stop executing or produce unexpected results. To mitigate this risk, it is important to properly handle errors and exceptions when executing the ping command in PHP.

// Execute ping command and handle errors
$host = 'example.com';
exec("ping -c 4 $host", $output, $return_code);

if($return_code !== 0) {
    // Handle error, log message, or take appropriate action
    echo "Ping command failed with error code: $return_code";
} else {
    // Process ping output
    foreach($output as $line) {
        echo $line . "\n";
    }
}