What are the advantages and disadvantages of using exec() or system() instead of ssh2_exec in PHP for server commands?

When executing server commands in PHP, using exec() or system() functions can be simpler and more straightforward compared to using ssh2_exec, which requires establishing an SSH connection. However, exec() and system() functions may pose security risks if not properly sanitized, as they allow for executing arbitrary commands on the server. On the other hand, ssh2_exec provides a more secure way to execute commands by utilizing SSH protocol, but it requires additional setup and configuration.

// Using exec() to execute a server command
$command = "ls -l";
exec($command, $output);
print_r($output);