What are some best practices for securely passing parameters to external applications in PHP?

When passing parameters to external applications in PHP, it is important to sanitize and validate user input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to achieve this is by using PHP's built-in functions like htmlspecialchars() or mysqli_real_escape_string() to sanitize input before passing it to external applications.

// Sanitize input before passing it to an external application
$user_input = $_POST['user_input']; // Assuming user input is coming from a form

// Sanitize the input using htmlspecialchars()
$sanitized_input = htmlspecialchars($user_input);

// Pass the sanitized input to the external application
external_application_function($sanitized_input);