What are the advantages and disadvantages of using sudo in a PHP script for executing commands with elevated privileges?

Using sudo in a PHP script for executing commands with elevated privileges can allow the script to perform tasks that require root access, such as restarting services or modifying system files. However, it also poses a security risk as it can potentially give unauthorized users access to sensitive system functions. It is important to carefully consider the potential risks and limitations before implementing sudo in a PHP script.

<?php

// Example code snippet using sudo in a PHP script
$command = "sudo service apache2 restart";
$output = shell_exec($command);

echo $output;

?>