How can external forms be used to input data into PHP arrays in a secure and efficient manner?

When using external forms to input data into PHP arrays, it is important to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One way to achieve this is by using PHP functions like filter_input() or htmlentities() to sanitize the input data before adding it to the array. Additionally, using prepared statements when interacting with a database can also help prevent SQL injection.

// Example of using filter_input() to sanitize input data before adding it to an array
$data = [];
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);

if ($name && $email) {
    $data['name'] = $name;
    $data['email'] = $email;
    
    // Add the data to the database using prepared statements
    // $stmt = $pdo->prepare("INSERT INTO table (name, email) VALUES (:name, :email)");
    // $stmt->bindParam(':name', $name);
    // $stmt->bindParam(':email', $email);
    // $stmt->execute();
}