How can a PHP developer determine if the command executed by exec() was successful or encountered an error?

When using the exec() function in PHP to run a command, the return value of exec() can be used to determine if the command was successful or encountered an error. If the command is successful, exec() will return the last line of the command output. If an error occurs, exec() will return false. To check for errors, you can use strict comparison operators to differentiate between false and the command output.

$command = 'ls -l';
$output = exec($command);

if ($output === false) {
    echo "An error occurred while running the command.";
} else {
    echo "Command executed successfully. Output: " . $output;
}