How can PHP developers ensure that shell commands are executed safely?

To ensure that shell commands are executed safely in PHP, developers can use the `escapeshellarg()` function to escape any user input before passing it to the shell. This function will properly quote and escape any special characters, preventing potential security vulnerabilities such as command injection attacks.

$user_input = $_POST['user_input'];
$escaped_input = escapeshellarg($user_input);
$output = shell_exec("ls " . $escaped_input);
echo $output;