How can one redirect error messages from shell commands executed in PHP to the output for better debugging?

When executing shell commands in PHP, error messages are often sent to the standard error stream instead of the standard output stream, making it harder to capture and debug them. To redirect error messages to the output for better debugging, you can use the 2>&1 syntax in the shell command to merge the standard error stream (2) with the standard output stream (1).

<?php
$command = 'your_shell_command 2>&1';
$output = shell_exec($command);
echo $output;
?>