What are some alternative approaches to using system() in PHP for executing external commands with better control and error handling?

Using `exec()` or `shell_exec()` functions in PHP can provide better control and error handling when executing external commands compared to `system()`. These functions allow you to capture the output of the command and handle errors more effectively.

// Using exec() to execute external command with better control and error handling
$output = [];
$return_var = 0;
exec('your_command_here', $output, $return_var);

if ($return_var !== 0) {
    // Handle error
    echo "Error executing command";
} else {
    // Process output
    foreach ($output as $line) {
        echo $line . PHP_EOL;
    }
}