What specific security measures can be implemented when handling form data in PHP?

When handling form data in PHP, it is important to implement security measures to prevent common vulnerabilities such as SQL injection and cross-site scripting (XSS) attacks. One way to enhance security is by using prepared statements when interacting with a database to prevent SQL injection attacks. Additionally, data validation and sanitization should be performed to ensure that only expected data is accepted and processed.

// Example of using prepared statements to prevent SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $_POST['username']);
$stmt->execute();
```
```php
// Example of data validation and sanitization
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);