What best practices should be followed when executing commands through PHP for external applications like TeamSpeak?

When executing commands through PHP for external applications like TeamSpeak, it is important to properly sanitize user input to prevent any potential security vulnerabilities. One common approach is to use escapeshellarg() or escapeshellcmd() functions to escape any user input before passing it to the external application.

// Example of executing a command for TeamSpeak with sanitized user input
$command = 'ts3server://example.com?port=' . escapeshellarg($_GET['port']);
$output = shell_exec($command);
echo $output;