How can PHP be used to troubleshoot issues with executing shell commands on a server?

When executing shell commands using PHP on a server, issues may arise due to permissions, path settings, or syntax errors. To troubleshoot these issues, you can use PHP's `exec()` function to run shell commands and capture any output or errors that may occur. By examining the output, you can identify the root cause of the problem and make necessary adjustments to resolve it.

// Example code to troubleshoot executing shell commands in PHP
$command = 'ls -l'; // Replace this with the shell command you want to execute
$output = array();
$return_var = 0;

// Use exec() function to run the command and capture output
exec($command, $output, $return_var);

// Check for errors or output
if ($return_var !== 0) {
    echo "Error executing command: $command\n";
    echo "Return code: $return_var\n";
} else {
    echo "Command executed successfully: $command\n";
    echo "Output: \n";
    foreach ($output as $line) {
        echo $line . "\n";
    }
}