What are some potential security risks when using PHP to execute shell commands?

One potential security risk when using PHP to execute shell commands is the possibility of command injection attacks. To mitigate this risk, it is important to properly sanitize and validate any user input before passing it to the shell command.

$user_input = $_POST['input'];

// Sanitize and validate user input
$validated_input = escapeshellarg($user_input);

// Execute shell command
$output = shell_exec("ls " . $validated_input);

echo $output;