What role does the test_input() function play in the PHP script, and how does it contribute to data sanitization and security?

The test_input() function in a PHP script plays a crucial role in data sanitization and security by removing any unwanted characters or tags from user input. This function helps prevent SQL injection attacks, cross-site scripting (XSS) attacks, and other forms of malicious input that could compromise the security of the application.

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

// Example usage:
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);