What are the best practices for securely executing commands on a server using PHP?

When executing commands on a server using PHP, it is important to ensure that the commands are executed securely to prevent any potential security vulnerabilities, such as command injection attacks. One way to securely execute commands is to use the `escapeshellarg()` function to escape any user input before passing it to the `shell_exec()` function.

// Securely execute command using escapeshellarg()
$user_input = $_POST['user_input'];
$escaped_input = escapeshellarg($user_input);
$output = shell_exec("command " . $escaped_input);
echo $output;