Are there alternative functions to shell_exec() that can be used in PHP for executing commands?

When using shell_exec() in PHP to execute commands on the server, there is a security risk as it allows for arbitrary shell commands to be executed. To mitigate this risk, it is recommended to use alternative functions such as exec(), passthru(), or proc_open() which provide more control over the execution of commands and can help prevent potential security vulnerabilities.

// Using exec() to execute a command safely
$output = array();
exec('ls -la', $output);
foreach($output as $line){
    echo $line . "<br>";
}