What are the best practices for executing commands or running programs from a PHP script?

When executing commands or running programs from a PHP script, it is important to use proper security measures to prevent any vulnerabilities. One common method is to use the `escapeshellcmd()` function to escape any special characters in the command string to prevent command injection attacks. Additionally, it is recommended to use absolute paths for the executable files to avoid any path traversal issues.

$command = '/path/to/executable';
$escaped_command = escapeshellcmd($command);
$output = shell_exec($escaped_command);

// Check for errors
if ($output === null) {
    echo "Error executing command";
} else {
    echo $output;
}