How can the issue of false return values be addressed when using the system() function for pinging in PHP?

When using the system() function for pinging in PHP, the issue of false return values can be addressed by using the exec() function instead. The exec() function returns the last line of the command output, which can be used to determine the success or failure of the ping operation. By parsing this output, you can accurately determine if the ping was successful or not.

$host = 'example.com';
$command = 'ping -c 4 ' . $host;
$output = exec($command);

if (strpos($output, '4 packets transmitted, 4 received') !== false) {
    echo 'Ping successful';
} else {
    echo 'Ping failed';
}